-1

I wrote a shell script that gets a value from a file and based on that value I want to echo a particular message. My console keeps on saying that there is an error on line 7 and 9. Any suggestions on how to fix it will be greatly appreciated.

export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home'
echo $JAVA_HOME
export CLASSPATH='/Users/edgarjohnson/Desktop/JarFiles/mlDownload.jar:/ddc/config'
echo $CLASSPATH
var=$(cat /ddc/config/LastRefreshDate.dat)
echo $var
if [$var > 0 ];then
        echo "Run Get Latest Update Class"
elif [$var = 0]; then
        echo "No need to run any updates"
fi
  • Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Nov 05 '18 at 16:50

1 Answers1

1

After [ and before ] there must be a space. Otherwise the variable is substituted and the shell will try to execute a program called [Whatever.

[ itself is actually just a binary which is executed with var's content, =, 0 and ] as arguments and its return code is used to determine whether the if or else branch should be taken.

However the operators used are not really the ones you intend to use, e.g. > is interpreted as shell redirect creating a file called 0 (or overwritting it) and is not actually comparing anything, use -gt instead. = checks string equality, -eq checks value equality.

As mentioned in the comments it may be better to use [[ ]] instead of [ ].

  • Worked like a charm Thanks – Edgar Johnson Nov 05 '18 at 15:56
  • Not quite. `>` is not an argument passed to `[`. `>` is a redirection operator, so `[$var > 0 ]` attempts to run the program identified by the string `[$var` with its output going to a newly created file named `0`. – William Pursell Nov 05 '18 at 16:03
  • Actually, you should use [[...]] which avoid the variable expansion and lower the need for quoting. Also, since it's a numerical comparison, it should be [[ $var -gt 0 ]] and [[ $var -eq 0 ]]. – Andre Gelinas Nov 05 '18 at 16:05
  • @WilliamPursell I have fixed that mistake –  Nov 05 '18 at 16:11
  • I recommend reading the bash manual too, https://www.gnu.org/software/bash/manual/bash.html. I know there's a lot there, but that is the best source for writing shell scripts. – Lewis M Nov 05 '18 at 16:11
  • Note that there is some dispute about `[[` vs `[`. Personally, I prefer neither, and use `[` spelled `test`. The problem with `[[` is that it does not generate useful error messages. If you try to do a numeric comparison with a string, for example. `[[ foo > 0 ]]` does not generate an error message, but `[ foo -gt 0 ]` does. Most people, however, seem to prefer `[[` despite this limitation. – William Pursell Nov 05 '18 at 16:20