1

I am writing a Shell Script where user will pass the path to JAVA_HOME as a parameter. I wanted to check whether the JAVA_HOME path is valid or not. So how can I check whether the input path passed is a exe file ( Unix Executable File ) or not ? Suppose if users passed /usr/bin/java - which is valid, if user passes like /usr/bin/test.txt ( some text file )- which is not valid. So how can I check whether the file is of type Unix Executable File ?

I tried with -x , guess that is not the rite check , since it returns true for /usr/bin also !!!

1
  • Post a snippet of your script where you are seeing the problem. Commented Jul 31, 2012 at 18:16

3 Answers 3

5

Easiest way is to use the file command.

$ file /usr/bin
/usr/bin: directory
$ file /usr/bin/java
/usr/bin/java: Mach-O universal binary with 2 architectures
/usr/bin/java (for architecture x86_64):    Mach-O 64-bit executable x86_64
/usr/bin/java (for architecture i386):  Mach-O executable i386
$ 

You can just grep the output of file for a suitable string, e.g. "executable".

Sign up to request clarification or add additional context in comments.

Comments

1

The man page for test is quite clear that just -x isn't going to do it

-x file True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be searched.

But if you combine this with -f and -r

-f file True if file exists and is a regular file.

-r file True if file exists and is readable.

you will know if the file has the requisite properties to be an executable.


There remains the possibility that a user could have set these properties on a non-executable, but that would clearly be a user error.


Man page excerpt here taken from the Mac OS 10.5 versions. You may seem something slightly different on other systems (Linux in particular).

Comments

0

Unix "executables" are anything that have the execute attribute set. You cannot rely on this since you can set execute permissions on a pdf document, for example.

Maybe what you ought to use to detect the type of file is the file command, instead. It will tell you whether the file is a binary or plain text, for example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.