Logical Error in Script Shell Shell

advertisements

Simple calculator that needs to quit when "quit" "q" "exit" or "x" is entered. What need to change in the condition?

echo -n "Enter Number Operator Number [exit/quit if finished]"
read num1 op num2

if [[ "${num1,,}" =~ "quit" | "q" ]] || [[ "${num2,,}" =~ "exit" | "x" ]]
then
    echo -e "\nExiting `basename $0`, Goodbye!\n"
    exit 0
fi


You are misinterpreting what "or" means. It is not an or between different values but between different logical conditions.

I think this is what you want:

[[ $num1 =~ "quit" || $num1 =~ "q" || $num2 =~ "exit" || $num2 =~ "x" ]]