2

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

2
  • 1
    You've asked 6 questions on the site, haven't accepted any answers, and only done 4 upvotes. Learn how to use the site or people will stop answering your questions. Commented Jun 23, 2010 at 14:36
  • Thanks for the comment. I will be doing this from now on. Commented Jun 24, 2010 at 6:10

4 Answers 4

7

It's probably simplest to use which:

which java || exit 1
Sign up to request clarification or add additional context in comments.

3 Comments

@Paul I see 14:13:59Z for both
Yeah, but yours shows up ahead of mine, so you must have hit the database ahead of mine.
@Paul Ah, I guess checking the ID would be the logical way to find out :)
7
if which java >/dev/null 2>&1 ; then
  echo yes
fi

Comments

1

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.

Comments

0

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.

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.