Detect Windows EXE type on linux

This shellscript helps to choose the execution environment for a windows executable on linux. Depending on the file signature it runs the file with WINE (for x86 and x64 applications), Mono (for .NET), or dosbox (for 16bit programs). As a secrity measure, only files with the execuatble flag will be launched.

 

#!/bin/sh
#
# runexe
# (c) 2012 ProgAndy
# Detects the required environment for MS executables and runs them accordingly

x64Command=wine
x86Command=wine
NetCommand=mono
DosCommand=dosbox

function stop_error {
	echo "$2." >&2
	if [[ ! -t 1 && -n "$(which Xdialog 2>/dev/null)" ]]; then
		# stdout is not a tty
		Xdialog --title "Run Windows executable" --msgbox "$2:\n$3" 8 80
	fi
	exit $1
}

# check for executable flag
exefile="`readlink -f \"$1\"`"
if [ ! -x "$exefile" ]
then
	stop_error 1 "File is not executable or file not found" "$1"
fi

# choose correct environment
type="$(file -b "$exefile")"
case "$type" in 
	*" (DLL"*)
		stop_error 2 "File is no valid windows executable" "$1" 
	;;
	*"Mono/.Net"*)
		# .NET
		#echo ".NET"
		$NetCommand "$@"
	;;
	*"PE32 executable"*)
		# X86
		#echo "x86"
		$x86Command "$@"
	;;
	*"PE32+ executable"*)
		# X64
		#echo "x64"
		$x64Command "$@"
	;;
	*"MS-DOS executable"*)
		# MS-DOS
		#echo "MS-DOS"
		$DosCommand "$@"
	;;
	*)
		stop_error 2 "File is no valid windows executable" "$1" 
	;;
esac

#wine "$@"