0
#!/bin/sh
hour=$1
minute=$2
if [ $hour -eq 12 ]
then
    echo "the time is $hour:$minute PM"
elif [ $hour -eq 24 ]
then
    hour=0
    echo "the time is $hour:$minute AM"
elif [ $hour -gt 12 ]
then
    let "b = $hour - 12" #
    echo "the time is $b:$minute PM"
else
    echo "the time is $hour:$minute AM"
fi

generally it's errors like:

jdoodle.sh: line 7: syntax error near unexpected token `elif'
jdoodle.sh: line 7: `elif [ $hour -eq 24 ]

or if that stops popping up it's syntax error near unexpected token `fi'

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • What's the exact command you use to run this? – Benjamin W. Apr 13 '20 at 00:35
  • The script works for me. Are you running the exact script you included in the post? Also, did you link `/bin/sh` to some non-POSIX compliant shell like fish? – Aplet123 Apr 13 '20 at 00:38
  • 1
    And also could be your file has dos line endings. – KamilCuk Apr 13 '20 at 00:42
  • this is my very first time using bash and I don't even know what is happening especially since this is our first and only lesson on using bash so I don't even understand what most of the errors mean – Jose Luis Angeles Rojas Apr 13 '20 at 00:46
  • we are using https://www.jdoodle.com/test-bash-shell-script-online/ to script and when I copy and paste the code I put here it still give me – Jose Luis Angeles Rojas Apr 13 '20 at 00:49
  • jdoodle.sh: line 7: syntax error near unexpected token `elif' jdoodle.sh: line 7: `elif [ $hour -eq 24 ] ' Command exited with non-zero status 2 – Jose Luis Angeles Rojas Apr 13 '20 at 00:50
  • Try https://shellcheck.net if there are no errors only warnings then tell your prof to drop that noodle-kadoodle site :-) – Jetchisel Apr 13 '20 at 00:51
  • When I copy-paste your script from your post into that site, it runs fine. Try it on a different browser, maybe? – that other guy Apr 13 '20 at 01:04
  • 1
    Since the `$hour` variable is missing double quotes in the tests, it expands globbing of file names with the content of it. I suspect the issue is when you run the script without providing arguments or providing wrong arguments. Add missing double quotes in all test like:`if [ "$hour"` and it shall fix your issue. – Léa Gris Apr 13 '20 at 01:19
  • *...or if that stops popping up it's...* What change did you make to make the first error "stop popping up"? – lurker Apr 13 '20 at 01:20
  • 1
    @JoseLuisAngelesRojas I'm able to reproduce that exact error message if I save this in a file with DOS/Windows line endings (and run it with the `sh` command, instead of just `./jdoodle.sh`). See ["Why is a shell script giving syntax errors when the same code works elsewhere?"](https://stackoverflow.com/questions/31886144/why-is-a-shell-script-giving-syntax-errors-when-the-same-code-works-elsewhere) and ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/31886144/why-is-a-shell-script-giving-syntax-errors-when-the-same-code-works-elsewhere) – Gordon Davisson Apr 13 '20 at 02:37
  • 1
    Mind you, you should *also* fix the quoting issues ([shellcheck.net](https://www.shellcheck.net) is good at pointing out mistakes like this). And it's best to make scripts executable, then run them with `./scriptname` instead of `sh scriptname`, so you don't override the shebang. If I do that with your script (with DOS/Windows line endings), I get the error "`-bash: ./jdoodle.sh: /bin/sh^M: bad interpreter: No such file or directory`" -- the `^M` represents a carriage return, which makes it a little easier to tell there's something weird in the file. – Gordon Davisson Apr 13 '20 at 02:41

0 Answers0