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"
Your code shows regular quotes
'…' and not back-ticks
hour=$(date | cut -c12-13)
The spacing matters between
-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 script.sh 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.