我想检查远程主机上是否存在某个文件。 我试过这个:
$ if [ ssh user@localhost -p 19999 -e /home/user/Dropbox/path/Research_and_Development/Puffer_and_Traps/Repeaters_Network/UBC_LOGS/log1349544129.tar.bz2 ] then echo "okidoke"; else "not okay!" fi
-sh: syntax error: unexpected "else" (expecting "then")
最佳答案
除了上面的答案,还有一个简写的方法:
ssh -q $HOST [[ -f $FILE_PATH ]] && echo "File exists" || echo "File does not exist";
-q
是安静模式,它会抑制警告和消息。
正如@Mat 提到的,像这样进行测试的一个优点是您可以轻松地将 -f
替换为您喜欢的任何测试运算符:-nt
、-d
, -s
等等...
测试运算符(operator): http://tldp.org/LDP/abs/html/fto.html
https://stackoverflow.com/questions/12845206/