Georgez Georgez - 1 year ago 72
Bash Question

Shell : Integer expression expected error

I am a beginner at programming in Unix enviroment and I am facing some difficulties at start. I am using PUTTY on Windows.
Here is my script and the code but when I run it, it tells me that

integer expression expected



if [ $hour -ge 0 -a $hour -lt 12 ]; then
echo "good morn"
elif [ $hour -lt 18 ]; then
echo "good afternoon"
echo "good night"

It seems that it doesn't work correctly with the pipeline or something; it doesn't translate the 'date' to the original date but takes it as a word, I think.

Answer Source

Your code shows regular quotes '…' and not back-ticks `…`. Use

hour=$(date | cut -c12-13)

The spacing matters between cut and -c12-13. In general, using $(…) is better than using back-ticks (see Pipe standard input and command line arguments in Bash for a discussion of why). Also, learn How to debug a Bash script, which means bash -x here.

With the code as written, the value in $hour is the string date|cut-c12-13 (not the output from the command) which is not an integer expression, hence the error message. Also, you most likely don't have a command called cut-c12-13, which is why the spacing matters there.