Linux 变量While循环
我想知道除了我已经包含的+之外,如何在下面的while循环中包含-、*、和/。如果用户输入的不是+、-、*或/我希望打印无效的输入消息。然而,到目前为止,我只知道如何在代码中包含一个参数,在本例中是+。如何在相同的代码位中包含其他3个参数?我承认我是一个不速之客,而且我目前没有词汇来搜索一个适合我需要的答案,所以我认为我最好的办法就是把问题写出来。 谢谢你的帮助。谢谢Linux 变量While循环,linux,bash,shell,while-loop,Linux,Bash,Shell,While Loop,我想知道除了我已经包含的+之外,如何在下面的while循环中包含-、*、和/。如果用户输入的不是+、-、*或/我希望打印无效的输入消息。然而,到目前为止,我只知道如何在代码中包含一个参数,在本例中是+。如何在相同的代码位中包含其他3个参数?我承认我是一个不速之客,而且我目前没有词汇来搜索一个适合我需要的答案,所以我认为我最好的办法就是把问题写出来。 谢谢你的帮助。谢谢 echo "Please enter an operation of arithmetic. Press either +, -
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
您可以在
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
您可能需要
在此处选择:
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
您缺少的搜索词汇表术语是“条件”。尝试搜索bash+conditional。查看select
命令,从选项列表中选择一个值。谢谢大家,是的,conditional就是我想要的,我会记住的