linux - 使用 ssh 检查远程主机上是否存在文件

我想检查远程主机上是否存在某个文件。 我试过这个:

$ 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/

相关文章:

linux - 如何将 bash 别名定义为多个命令的序列?

linux - 从标准输出中获取输出的最后 4 个字符

php - 使路径在 linux 和 Windows 上都有效

linux - 将时间跨度(以秒为单位)转换为 shell 中的格式化时间

linux - 如何安排 tcpdump 在特定时间段内运行?

linux - 须藤 : docker-compose: command not found

linux - 无法在 Ubuntu 16.04 中启动 Docker 服务

linux - 当命令太长时,命令行会自动覆盖

linux - "In-source builds are not allowed"在 cmake

java - 无法创建Java虚拟机