[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
posted on 2020-03-19 22:50  InnoLeo  阅读(334)  评论(0编辑  收藏  举报