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
#!/bin/bash
hour='date|cut-c12-13'
if [ $hour -ge 0 -a $hour -lt 12 ]; then
echo "good morn"
elif [ $hour -lt 18 ]; then
echo "good afternoon"
else
echo "good night"
fi
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.
-aas an "and" operator is explicitly marked obsolescent in the current version of the POSIX spec. Use[ "$hour" -ge 0 ] && [ "$hour" -lt 12 ]instead, if you want to be POSIX-compliant, or(( hour >= 0 && hour < 12 ))to leverage modern (much more readable) bash-specific syntax.OBflag next to-aand-omarks them as obsolescent, and theXSIflag marks them as an extension to the baseline POSIX standard (so not all systems need to support them to begin with).[ $hour -ge 0 ]and[ "$hour" -ge 0 ]are not the same thing: the latter allows for better error messages in many common cases, and prevents outright incorrect results in a rarer set of conditions. shellcheck.net will catch that variety of mistake for you).hour=$(date +'%H)'rather than usingcut... and it seems unlikely you could get a negative hour so the test for less than zero is a bit odd too.