[shell] shell脚本中的条件测试表达式

远程测试

ssh host "test -e /path/to/file"
if [ $? -eq 0 ]; then
    echo " your file exists"
fi

ssh remote_host test -f"/path/to/file" && echo found || echo not found
 

方法

  1. []
  2. [[]]
  3. (())
  4. test
[root@red11d ~]# [ -d /root ] && echo 0 || echo 1
0
[root@red11d ~]# [[ -d /root ]] && echo 0 || echo 1
0
[root@red11d ~]# [[ -d /root ]] && {
> echo 0
> echo 1
> }
0
1
[root@red11d /]# ff=$(((-d /boot)) && echo 0 || echo 1)
-bash: ((: -d /boot: division by 0 (error token is "boot")
[root@red11d /]# echo $ff
1

 

[root@red11d ~]#  d=`test -d /root && echo 0 || echo 1`
[root@red11d ~]# echo $d
0
[root@red11d ~]# dd=$(test -d /root && echo 0 || echo 1)
[root@red11d ~]# echo $dd
0
if thing ; then ... ; else ... ; fi
每天进步一点点,多思考,多总结 版权声明:本文为CNblog博主「zaituzhong」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文地址:https://www.cnblogs.com/tingxin/p/12527923.html