简体   繁体   中英

Variable While Loop

I would like to know how to include -, *, and ,/, in the following while loop in addition to the + I have already included. If the user enters something other than +, -, * or / I want the invalid input message to print. However, so far I have only worked out how to include one of the arguments in the code, in this case the +. How do I include the other 3 arguments in the same bit of code? I am a noobie I admit, and I don't currently have the vocabulary to search an answer specific to my needs so thought my best best was writing out the issue. Any help appreciated. Thanks

echo "Please enter an operation of arithmetic. Press either +, -, * or /"

read operation
while [ $operation != "+" ]; do

echo "sorry, that is an invalid input- re-enter operation of arithmatic"
read operation

You can use that in a while loop like this:

while read -p "Please enter an operation of arithmetic. Press either +, -, * or /: " op &&
     [[ $op != [-+/*] ]]; do
   echo "sorry, that is an invalid input- re-enter operation of arithmatic"
done

You probably want select here:

PS3="Please enter an operation of arithmetic: "
select op in + - / '*'; do
    case $op in 
        -) echo subtract something ; break ;;
        +) echo add something ; break ;;
        /) echo divide something ; break ;;
       \*) echo multiply something ; break ;;
    esac
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM