【发布时间】:2020-07-16 08:09:30
【问题描述】:
我正在尝试检查文件是否存在于 Python / Linux 中。
我的本地机器(我通过 ssh 使用)是:Red Hat Enterprise Linux Server。
目标机器(我 ssh 到)是:Amazon Linux、centos rhel fedora。
我尝试过以下 CMD:
ssh -qnx [host] "test /path/to/file.txt && echo 'Exists' || echo 'Not'"
但它总是返回“Exists”,在不存在的文件上也是如此。
这个 CMD 也总是返回存在:
ssh host test -f "/path/to/file.txt" && echo found || echo not found
还有这个:
if ssh [host] test -e "/path/to/file.txt" ; then echo found ; else echo not found ; fi
我也试过了:
ssh -q [host] [[ -f /path/to/file.txt ]] && echo "File exists" || echo "File does not exist";
但它说:zsh: bad pattern: [[
我需要一个简单的 CMD,稍后我可以在 python 中使用os.system(cmd),并收到是或否的答案。
我错过了什么?
谢谢
【问题讨论】:
-
"test /path/to/file.txt && echo 'Exists' || echo 'Not'"不应该是test -f吗?修复后它对我有用。 -
它还为我返回不存在的文件的“找到”。我怀疑我的本地机器的行为与普通 linux 不同。
-
ssh -qnx user@host "test -e /path/to/file && echo exists || echo not"。您在test命令中缺少-e或-f。 -
或者,
ssh -qnx user@host "test -e /path/to/file" && echo exists || echo not -
ssh -qnx user@host "test -e /path/to/file && echo exists || echo not" - 如果你想添加它作为答案,我会接受它。 :-)
标签: linux ssh filesystems remote-access