变量循环

时间:2015-09-14 14:44:10

标签: linux bash shell while-loop

我想知道如何在以下while循环中包含 - ,*和/,以及我已经包含的+。如果用户输入+, - ,*或/以外的内容,我希望打印无效的输入消息。但是,到目前为止,我只研究了如何在代码中包含一个参数,在本例中为+。如何在同一位代码中包含其他3个参数?我承认,我是一个noobie,我目前没有词汇来搜索特定于我需求的答案,所以我认为最好的是写出问题。 任何帮助赞赏。感谢

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

2 个答案:

答案 0 :(得分:0)

您可以在while循环中使用它,如下所示:

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

答案 1 :(得分:0)

您可能需要select

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