-3

The next phase of this is supposed to check to see if either number entered is indeed a number or it quits the script and asks for a number. The code looks ok in ShellCheck (no errors) but regardless of what is put in, it thinks you didn't enter a number and it kills the script (good news, the exit works).

#!/bin/bash
echo "Please input a whole number"
read -r num1

if [ "$num1" = [0-9] ]; then echo "Please input another whole number" read -r num2; else echo "Please enter a whole number only"; exit fi

if [ "$num2" = 0-9 ]; then echo "Please input function: [a] for addition, [s] for subtraction, [m] for multiply, or [d] for divide"; read -r func else echo "Please enter whole number only" exit fi

if [ "$func" = "a" ]; then echo "The sum of the two numbers is " $((num1 + num2)) elif [ "$func" = "s" ]; then echo "The difference of the two numbers is " $((num1 - num2)) elif [ "$func" = "m" ]; then echo "The product of the two numbers is "$((num1 * num2)) elif [ "$func" = "d" ]; then echo "The quotient is (part of answer here) with a whole number remainder of (answer her) "$((num1 / num2)) else echo "Please select only [a] for addtion, [s] for subtration, [m] for multiply, or [d] for divide" fi

ilkkachu
  • 138,973

1 Answers1

0

You're trying to match a regular expression using an equality operator =, which isn't going to work. You're also using [ which doesn't support regular expression comparisons, you need to use [[ ... ]] instead. And even if the rest of your code worked, your attempted regex would match any string that contained at least one digit (i.e. it would match not only numbers like 123 but also non-numbers like abc123xyz)

Try something like this for num1:

while read -p 'Please input a whole number: ' -r num1 ; do
  if [[ $num1 =~ ^[0-9]+$ ]] ; then 
    break
  else
    echo "Invalid input, try again."
  fi
done

And do something similar for num2. and maybe for func with the regex ^[asmd]$.

This will loop forever until something inside the loop (i.e. a successful match against the ^[0-9]$ regex) exits the loop (i.e. executes break).

or, slightly shorter:

while read -p 'Please input a whole number: ' -r num1 ; do
  [[ $num1 =~ ^[0-9]+$ ]] && break || echo "Invalid input, try again."
done

test-something && what-to-do-if-true || what-to-do-if-false is a convenient shortcut when you don't want to type out a complete if/then/else/fi construct for a simple test. You can do more inside an if/then/else/fi, but this is useful when you only need to do one thing on a success or failure. BTW, the || what-to-do-if-false part is optional, and there are several other variant ways this can be used.

Note that every command you run will exit with either a zero (true/success) or non-zero (false/failure/error) exit code. Non-zero exit codes range from 1 to 255 and can indicate the type of error (there are some commonly used exit codes for things like "file not found" or "access denied" etc, but exit codes and their meanings are mostly dependent on the program being run). So you can use this short conditional test to ensure that one command only executes if the immediately previous command succeeded. e.g.

cd mydir && rm myfile

or

grep -q regex file.txt || mv file.txt /some/directory/

the rm will only be executed if the cd succeeded, and file.txt will only be moved if it doesn't contain regex.

cas
  • 78,579
  • Thanks Cas! Again I understand even more now. I learned that I can add && [[ $num1 -ge 1 ]] to the line in order to prevent someone enter a zero, but not preventing them from entering something like '10' or '1000'. I originally try it with ^[1-9] but it blocked any thing with a zero in it. The shell can be unforgiving. – Geordie Calhoun Dec 08 '21 at 18:33