Shell 里的条件表达式
shell为GNU bash 3.2.48
我想要求用户输入数字5或者6作为参数,本人参考的是鸟哥的入门课程,里面没有(目前为止)提及条件的结合,所以做过多种错误尝试:
1 # incorrect ones 2 #while [ -z "$input" || ( ! "$input" == "5" && ! "$input" == "6" ) ] 3 #while [ -z "$input" || \( ! "$input" == "5" && ! "$input" == "6" \) ] 4 #while [ -z "$input" ] || [ "$input" -ne "5" && "$input" -ne "6" ] 5 #while [ -z "$input" ] || [ "$input" != "5" && "$input" != "6" ] 6 7 # correct ones 8 #while [ -z "$input" ] || [ "$input" != "5" -a "$input" != "6" ] 9 #while [ -z "$opt" ] || [[ "$opt" != "55" && "$opt" != "6" ]] 10 while [ -z "$opt" ] || (( $opt != 55 && $opt != 6 )) 11 do 12 read -p "Pelase select the version (5/6) : " input 13 done
简单总结如下:
1. 判断符号[],单层的时候,内容中不可使用&&, ||等符号。
2. 括号()只能双层使用,内容中的variable会被替换成值之后再做比较。(只能是数值的比较?)
还有未解决的问题,如以下嵌套的条件组合如何在bash一条语句中实现?
( a != 1 ) && ( b != '' || ( b != '3' && d != '4'))