tags:

views:

53

answers:

4

On a unix machine, how can I write a shell script for checking if 'java bin' directory has been included in $PATH env. variable?

+6  A: 

It's probably simplest to use which:

which java || exit 1
Michael Mrozek
You beat me by seconds! :-)
Paul Tomblin
@Paul I see 14:13:59Z for both
Michael Mrozek
Yeah, but yours shows up ahead of mine, so you must have hit the database ahead of mine.
Paul Tomblin
@Paul Ah, I guess checking the ID would be the logical way to find out :)
Michael Mrozek
+5  A: 
if which java >/dev/null 2>&1 ; then
  echo yes
fi
Paul Tomblin
+1  A: 

Since the directory name can be anything, this would be a bit hard to check by looking at the $PATH variable, but you could try looking at the return value of a command like which javac.

Miel
A: 

which is appropriate, but only checks for your command defined in $PATH. What if javac is not defined in $PATH but it is installed somewhere else? In that extreme case, you use find or locate to find where javac is.

ghostdog74