Abhishek
Abhishek

Reputation: 879

Shell Scripting - check java bin

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

Upvotes: 2

Views: 2568

Answers (4)

ghostdog74
ghostdog74

Reputation: 342363

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.

Upvotes: 0

Miel
Miel

Reputation: 3477

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.

Upvotes: 1

Paul Tomblin
Paul Tomblin

Reputation: 182782

if which java >/dev/null 2>&1 ; then
  echo yes
fi

Upvotes: 7

Michael Mrozek
Michael Mrozek

Reputation: 175375

It's probably simplest to use which:

which java || exit 1

Upvotes: 7

Related Questions