0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Georgez
  • 53
  • 9
  • 1
    As a further aside, using `-a` as 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. – Charles Duffy Jun 01 '16 at 14:45
  • 1
    ...see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html for the spec for POSIX test; the `OB` flag next to `-a` and `-o` marks them as obsolescent, and the `XSI` flag marks them as an extension to the baseline POSIX standard (so not all systems need to support them to begin with). – Charles Duffy Jun 01 '16 at 14:46
  • 1
    (oh -- and quoting matters; `[ $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. http://shellcheck.net/ will catch that variety of mistake for you). – Charles Duffy Jun 01 '16 at 14:48
  • Surely you would want `hour=$(date +'%H)'` rather than using `cut`... and it seems unlikely you could get a negative hour so the test for less than zero is a bit odd too. – Mark Setchell Jun 01 '16 at 15:22

1 Answers1

4

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


As the comments to the question show, there are multiple other issues that should also be addressed, from generating the information you want directly from the date command to using double quotes around variable references, not using deprecated operators -a and -o with [, and there are those who'd argue that you should use [[ … ]] in preference to [ … ], or the Bash-specific (( … )), etc.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • It might be worth explaining that `$()` is better than backticks because it can be easily nested and because quoting is easier to deal with. And because people reading your code don't inadvertently think that you typed single-quotes. :) – dannysauer Jun 01 '16 at 14:45
  • 1
    @dannysauer: I've added an x-ref to a question where that issue is discussed. – Jonathan Leffler Jun 01 '16 at 14:53
  • thank you very much, understood. as far as bash -x script.sh i have made chmod +x so its ok i thing – Georgez Jun 01 '16 at 15:33
  • 2
    @Georgez: Note that `bash -x script.sh` gives you a trace of what the script is doing as it is run; it is equivalent to running `set -x` as a statement within the script. It is independent of the permissions on the script. – Jonathan Leffler Jun 01 '16 at 15:58