if ssh server [ -d /path/to/dir1 && -d /path/to/dir2 && -d /path/to/dir3 && -d /path/to/dir1 ]
谁能帮帮我,因为我是bash新手。
[ ]
是调用内置POSIXtest
shell的语法糖。不允许&&
进入
[[ ]]
是bash扩展,它允许&&
生成复合逻辑表达式。
所以有两种方法可以做你想做的。
POSIX方式:
if [ -d /path/to/dir1 ] && [ -d /path/to/dir2 ] && [ -d /path/to/dir3 ] && [ -d /path/to/dir1 ]
then
# Your code here
fi
使用bash[[ ]]
扩展:
if [[ -d /path/to/dir1 && -d /path/to/dir2 && -d /path/to/dir3 && -d /path/to/dir1 ]]
then
# Your code here
fi
在你的情况下,它看起来像你是ssh
到服务器,然后试图看看是否所有的条件都是真的。在这种情况下,在POSIX shell中你可以做
if ssh server sh -c '[ -d /path/to/dir1 ] && [ -d /path/to/dir2 ] && [ -d /path/to/dir3 ] && [ -d /path/to/dir1 ]'
then
# Your code here
fi
(如果您知道用户在远程主机上的登录shell是POSIX shell,则sh -c
不是严格必要的)
使用bash扩展,您可以执行
if ssh server bash -c '[[ -d /path/to/dir1 && -d /path/to/dir2 && -d /path/to/dir3 && -d /path/to/dir1 ]]'
then
# Your code here
fi
在最后两个例子中,引号都是必要的。