Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 不止一个if-BASH_Linux_Bash - Fatal编程技术网

Linux 不止一个if-BASH

Linux 不止一个if-BASH,linux,bash,Linux,Bash,如何在bash中创建多个if函数??例如,我想做这样的事情: if [ "$answer" == "0" ]; then 0 if [ "$answer" == "1" ]; then 1 if [ "$answer" == "2" ]; then 2 else echo "error" fi 谢谢您可以使用elif 如果要使用相同的测试$answer==则最好使用用例: 您可以在这里找到许多关于Bash脚本的有价值的东西: 别忘了男人狂欢节 好的,您可以从使用google开始:Delif可能

如何在bash中创建多个if函数??例如,我想做这样的事情:

if [ "$answer" == "0" ]; then 0
if [ "$answer" == "1" ]; then 1
if [ "$answer" == "2" ]; then 2
else echo "error"
fi
谢谢

您可以使用elif


如果要使用相同的测试$answer==则最好使用用例:

您可以在这里找到许多关于Bash脚本的有价值的东西:

别忘了男人狂欢节
好的,您可以从使用google开始:Delif可能是您想要的东西。[…==…]不符合标准,请对支持它的shell使用[…=…]或[…==…],但不要混合使用.And-eq,如果您希望它们被视为整数,也就是说,如果您希望01等于1Skip,请参阅高级Bash脚本指南。它包含了许多不好的建议。
if [ "$answer" == "0" ]; then echo "0"
elif [ "$answer" == "1" ]; then echo "1"
elif [ "$answer" == "2" ]; then echo "2"
else echo "error"
fi
case "$answer" in
  0) echo "0" ;;
  1) echo "1" ;;
  2) echo "2" ;;
  3|4) echo "3 or 4" ;;
  *) echo "error" ;;
esac