一般我们使用如下语法
if [ expression ] theniflinux中提供了高级的双括号语法。
在之前,如果要判断数字大小,大于号需要转义,否则会被认为重定向
举例:
程序解释:上面程序执行,虽然输出了10>8,但不是真正意义的比大小,而是认为了重定向,生成了文件名8。
[root@localhost shell]# ./test1.sh 10>8[root@localhost shell]# ls8 test1.sh以上通过转义即可解决问题。
[root@localhost shell]# cat test1.sh #!/atgdbm/bashnum=10if [ num > 8 ];thenecho "10>8"fi 双括号语法 [root@localhost shell]# cat test1.sh #!/atgdbm/bashnum=10if (( $num ** 2 > 90 ));then(( result = $num ** 2 ))echo "10的平方="$resultfi[root@localhost shell]# ./test1.sh 10的平方=100大于号不需要进行转义,写起来也简单些。
双方括号语法双方括号里的expression使用了test命令中采用的标准字符串比较。但它提供了test命令未提供的另一个特性,模式匹配。
[root@localhost shell]# cat test1.sh #!/atgdbm/bashif [[ $USER == r* ]]thenecho "hello $USER"elseecho "Sorry,you not start with r"fi[root@localhost shell]# ./test1.sh hello root