简体   繁体   English

为什么带有 -c 选项的 grep 总是在 ssh 上显示 0

[英]Why does grep with the -c option always show 0 over ssh

I have a script on three different servers.我在三个不同的服务器上有一个脚本。 The basics are that script checks for a certain file on its system and if the file is not there then it will ssh to the other servers and run the same script.基础是脚本检查其系统上的某个文件,如果该文件不存在,那么它将 ssh 到其他服务器并运行相同的脚本。 The check is run via;检查通过以下方式运行;

If [[ $(ls ${folder} | grep -c ${file}) -gt 0 ]]; then "do stuff with the files"; else "ssh to other server and check again"; fi

It wasnt giving output when the information was on a remote server.当信息在远程服务器上时,它没有给出 output。 So I ran the check on a server I knew had the files and it worked.所以我在我知道有文件的服务器上运行了检查,它工作正常。 However when i would try and run it remotely, the file count would be 0.但是,当我尝试远程运行它时,文件数将为 0。

I changed it from grep to wc -l and then it worked.我将它从 grep 更改为 wc -l 然后它工作了。 I'm wondering if anybody knows why grep -c works locally but with ssh我想知道是否有人知道为什么 grep -c 在本地工作但使用 ssh

It 'if' doesn't work like that.它'如果'不是那样工作的。 If you want to check the existence of a file, you must use this:如果你想检查一个文件是否存在,你必须使用这个:

[ -f ${file} ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM