How to get path and check in if statement

Hi,

I was wondering if it possible to get the path of a variable and compare that to something. Basically I want to write a script that checks if my $JAVA_HOME is correct and if not then it sets it. So far I have...

if [[$JAVA_HOME != '/pathhere']]
then
export JAVA_HOME='/pathhere'
echo JAVA_HOME='/pathhere'
fi

Problem is that the script can't seem to understand that I want to compare the path $JAVA_HOME and not what's in there. Anyone know how to just extract the path from that already set variable? Thanks!

Your question doesn't make sense. Your code already compares the value of $JAVA_HOME to the string /pathhere; how this is or isn't "what's in there" is not quite clear.

Depending on your shell, you might need to add spaces inside the [[ ... ]] delimiters in order for the syntax to validate. Is that your problem?

Speculatively, if you want to check if a colon-delimited path contains /pathhere anywhere in it, and if not, add it, try this:

case :$JAVA_HOME: in *:/pathhere:*) ;; *) JAVA_HOME=/pathhere:"$JAVA_HOME";; esac

well reason why im confused is that when i tried to run the script it errors in the if condition. it gives me this...

/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home: No such file or directory

where that path is the $JAVA_HOME. It shouldn't matter if that file or directory exist right, I just want to check that value which is the problem

Try adding those spaces, and also quote the variable. (case is less picky about quoting and works even in good ole /bin/sh which doesn't have this fancy [[ ... ]] stuff.)

case $JAVA_HOME in /pathhere) ;; JAVA_HOME=/pathhere;; esac

ok cool I tried the space thing and it worked! Thanks!

How exactly does case work? Is it like if the first element is a part of the second element then do then else this? Sorta like that?

No, it's basically a generalization of if then else if then else if then else, but all branches examine the same variable. In many programming languages, this is a "switch" statement (think railroad switch). Read the documentation for your shell.

test -e $JAVA_HOME

or use if statement.