I'm just getting started on a script that does basic shell commands, but the second function, which is supposed to take a valid year and month and plug them into cal, is returning with this error:

Enter the year:
/home/duncan/menuscript.sh: line 34: [2014: command not found
/home/duncan/menuscript.sh: line 34: [2014: command not found
This is an invalid year

This is the code I am working on, I have looked for the answer already, and I cant figure out why it wont compile.. I am not trying to enter the date as a command!

echo "Welcome to Duncan's main menu"
function press_enter
    echo ""
    echo -n "Press Enter to continue"
until [ "$selection" = "9" ] 
  echo ""
  echo "    1 -- Display users currently logged in"
  echo "    2 -- Display a calendar for a specific month and year"
  echo "    3 -- Display the current directory path"
  echo "    4 -- Change directory"
  echo "    5 -- Long listing of visible files in the current directory"
  echo "    6 -- Display current time and date and calendar"
  echo "    7 -- Start the vi editor"
  echo "    8 -- Email a file to a user"
  echo "    9 -- Quit"
  echo ""
  echo "Make your selection:"
  read selection
  echo ""
  case $selection in
    1) who ; press_enter ;;
    2) echo "Enter the year:" ;
       read year ;
       if [$year>1] || [$year<9999]; then
         echo "Enter the month:" 
         read month
         if [0<$month<12]; then
           cal -d $year-$month 
           echo "This is an invalid month"
          echo "This is an invalid year"
       fi ;;

    9) exit ;;
    *) echo "Please enter a valid option" ; press_enter ;;
  • without looking too close, since you're using #!/bin/bash anyway, why not use select rather than case here? It does pretty much all of that automatically, I think. Though, if I recall correctly - (not guarantee), bash can sometimes trip up *) rather than (*).... Oh yeah, the issue is probably here: if [0<$month<12] - you want some spaces and quotes I think... – mikeserv Dec 9 '14 at 19:26
  • Put a space between the [ and the following text. This ought to fix it – daniel kullmann Dec 9 '14 at 19:29
  • @mikeserv Spaces and quotes alone will not make it work. – Hauke Laging Dec 9 '14 at 19:32
  • @HaukeLaging - probably not. – mikeserv Dec 9 '14 at 19:33
  • @danielkullmann No. – Hauke Laging Dec 9 '14 at 19:34

There is a (common) syntax error in this line (OK, not only one...):

  1. [ is not a special character but an ordinary command. Thus the rest of the line is parameters and those must be separated by whitespace: [ "$year" > 1 ]

  2. The next problem is that > is not an ordinary parameter but the redirection character (a metacharacter). Thus the shell sees (e.g.) [2014 and looks for a command with this name. If there was one then the shell would write its output to the file 1]...

If you use [ ... ] then you need the operator -gt (greater than):

[ "$year" -gt 1 ]

An alternative is to use the bash reserved word [[ instead of [. You still have to use the whitespace as separator but you can omit the quotes:

[[ $year -gt 1 ]]

or you can use arithmetic expressions to compare integers:

((year > 1))
  • Thanks! However, right before you posted this I tried just adding the spaces, and it worked without the double brackets – Duncan Dec 9 '14 at 19:35
  • @Duncan It is not working (the way you expect) if you keep using > and <. And 0<$month<12 is not working at all, not even with [[. – Hauke Laging Dec 9 '14 at 19:37
  • I split it up into $month>0 and $month<12 and it works smoothly. Also, you were right about [[ with only [ it gave the correct cal but with a small error above – Duncan Dec 9 '14 at 19:38
  • @Duncan Then you may accept this answer so that the readers of this site see that your problem is solved. – Hauke Laging Dec 9 '14 at 19:42
  • 1
    @Duncan Note that you cannot use > inside double brackets either. Well, you can, syntactically speaking, but it doesn't do what you might think it does: it compares strings, not numbers, so e.g. [[ 10 > 2 ]] is false. You can use familiar arithmetic syntax inside double parentheses. – Gilles Dec 9 '14 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.