Linux Shell脚本编程case条件语句
1,判断一个数字是否则在1,2,3之中. #!/bin/bash read -p "pls input a number:" n case "$n" in 1) echo "变量是1" ;; 2) echo "变量是2" ;; 3) echo "变量是3" ;; *) echo "pls input a number between 1 and 3" exit; esac 2,多级 if语句改写 #!/bin/bash read -p "pls input a number:" n if [ $n -eq 1 ]; then echo "$n是变量1" elif [ $n -eq 2 ]; then echo "$n是变量2" elif [ $n -eq 3 ]; then echo "$n是变量3" else echo "pls input a number between 1 and 3" fi 3,if..else嵌套,实现 #!/bin/bash read -p "pls input a number:" n if [ $n -eq 1 ]; then echo 1 else if [ $n -eq 2 ]; then echo 2 elif [ $n -eq 3 ]; then echo 3 else echo "pls input a number [1-3]" fi