Take a look at the various test statements (this is for the test command itself, but the built-in BASH and TCSH tests are more or less the same).
You will notice that -x FILE says that the FILE exists, and permission to execute (or search) is granted.
BASH, Bourne, Ksh, Zsh Script
if [[ -x "$file" ]] then echo "File '$file' is executable" else echo "File '$file' is not executable or found" fi
TCSH or CSH Script:
if ( -x "$file" ) then echo "File '$file' is executable" else echo "File '$file' is not executable or found" endif
To determine the type of file, try file . You can parse the output to see what type of file it has. Word 'o Warning : Sometimes file returns more than one line. Here's what happens on my Mac:
$ file /bin/ls /bin/ls: Mach-O universal binary with 2 architectures /bin/ls (for architecture x86_64): Mach-O 64-bit executable x86_64 /bin/ls (for architecture i386): Mach-O executable i386
The file command returns different output depending on the OS. However, the word executable will be in executable programs, and usually the architecture will also appear.
Compare the above with what I get in my Linux box:
$ file /bin/ls /bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped
And the Solaris window:
$ file /bin/ls /bin/ls: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped
In all three cases, you will see the word executable and architecture ( x86-64 , i386 or SPARC using 32-bit ).
Adding
Thank you very much, this seems like a way. Before marking this as your answer, can you tell me what kind of shell script check I need to perform (for example, what kind of parsing) on โโthe โfileโ to check if I can execute the program? If such a test is too difficult to do on a general basis, I would at least want to check whether it is linux or osX executable (Mach-O)
At the top of my head, you can do something like this in BASH:
if [ -x "$file" ] && file "$file" | grep -q "Mach-O" then echo "This is an executable Mac file" elif [ -x "$file" ] && file "$file" | grep -q "GNU/Linux" then echo "This is an executable Linux File" elif [ -x "$file" ] && file "$file" | grep q "shell script" then echo "This is an executable Shell Script" elif [ -x "$file" ] then echo "This file is merely marked executable, but what type is a mystery" else echo "This file isn't even marked as being executable" fi
Basically, I run the test, and then if it succeeds, I do grep on the output of the file command. grep -q means no print is output, but use the grep exit code to find out if I found a string. If your system does not accept grep -q , you can try grep "regex" > /dev/null 2>&1 .
Again, the output of the file command may differ from system to system, so you will need to make sure that they will work on your system. In addition, I check the executable bit. If the file is a binary executable, but the executable bit is not included, I will say that it is not executable. Perhaps this is not what you want.