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
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
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.