0

I am trying to escape the * character when running this calculator script, but when I run it, it does not output an error, but simply exits the script.

./calculator 3 * 4 should print Usage-./calculator.sh .... , but instead it just exits. I am not allowed to use \* between the numbers. Tried everything possible according to me but I can't seem to make it work. Any help would be appreciated.

Also, I'm ssh'ing to my school server from my Macbook, would that be an issue?

#!/bin/bash -f
if [ "$#" == "0" ] || [ "$2" == '*' ]
then
    echo Usage-./calculator.sh value1 operator value2
    echo where,
    echo value1: numeric value
    echo value2: numeric value
    echo operator: one of +,-,/,x
elif [ "$2" == "+" ]
then
    echo $(( $1 + $3 ))
elif [ "$2" == "-" ]
then
    echo $(( $1 - $3 ))
elif [ "$2" == "x" ]
then
    echo $(( $1 * $3 ))
Major
  • 544
  • 4
  • 19
Nabeel
  • 1
  • 2
  • 1
    `./calculator 3 * 4` has the `*` replaced with a list of filenames **before your script is even started** (in fact, before the new copy of bash for the script even begins to run). There's nothing you can do inside that script to prevent that from happening, because it's already too late. – Charles Duffy Jan 31 '18 at 17:21
  • ...and btw, `[ "$foo" == "$bar" ]` should be `[ "$foo" = "$bar" ]`; see specification at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html -- you'll see `=` is the only standardized string comparison operator. – Charles Duffy Jan 31 '18 at 17:23
  • Change your logic to check that you have 3 arguments where the middle one is a valid operator. That will print the help message when using `*` because it will not be a valid expression – that other guy Jan 31 '18 at 17:37

0 Answers0