linux - 如果我通过 ssh 执行命令,则无法获得正确的输出

标签 linux unix

以下命令的输出

sudo find / -xdev -type f -size +100000 -name "*.log" -exec gzip -v {} \; 2>&1 | awk '{print $6}'

给我

/opt/testing3/cat1.log.gz
/opt/testing3/cat2.log.gz
/opt/testing3/cat3.log.gz
/opt/testing3/cat4.log.gz
/opt/testing3/cat5.log.gz
/opt/testing3/cat6.log.gz
/opt/testing3/cat7.log.gz

但是,如果我通过 ssh 执行此操作,它会给我:

 ssh user@hostname "sudo find / -xdev -type f -size +100000 -name \"*.log\" -exec gzip -v {} \; 2>&1 | awk '{print $6}'"

结果

/opt/testing3/cat1.log:  95.4% -- replaced with /opt/testing3/cat1.log.gz
/opt/testing3/cat2.log:  95.4% -- replaced with /opt/testing3/cat2.log.gz
/opt/testing3/cat3.log:  95.4% -- replaced with /opt/testing3/cat3.log.gz
/opt/testing3/cat4.log:  95.4% -- replaced with /opt/testing3/cat4.log.gz
/opt/testing3/cat5.log:  95.4% -- replaced with /opt/testing3/cat5.log.gz
/opt/testing3/cat6.log:  95.4% -- replaced with /opt/testing3/cat6.log.gz
/opt/testing3/cat7.log:  95.4% -- replaced with /opt/testing3/cat7.log.gz

为什么输出不同?似乎 awk 没有被执行。

最佳答案

你必须对“$”符号进行转义,这样才不会在本地获取,而是通过远程传递。

$ ssh $HOSTNAME "ls -l /tmp | tail -1 | awk '{print $6}'"
-rw-r--r-- 1 root     root       0 Apr 29 01:31 file.log
$ ssh $HOSTNAME "ls -l /tmp | tail -1 | awk '{print \$6}'"
Apr

关于linux - 如果我通过 ssh 执行命令,则无法获得正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939634/

相关文章:

unix - 获取 Go 语言中的 Unix 时间戳(当前时间,以秒为单位)

linux - mkdir选项不清楚

linux - 使用 shell 脚本合并最近 7 天的日志文件

linux - 如何创建将备份当前目录并将存档发送到 FTP 服务器的 cronjob?

c - 在 Linux/Unix 中用 C 语言管理进程 fork

c - 当我有 AF_UNIX 的 socket() 时,为什么我需要 socketpair()?

C++ 在 execv 中运行 grep 搜索可执行路径

linux - 差不多了!如何向 bash 提示符添加另一种 git status 颜色?

java - 使用unix语法运行java类文件?

c - 父进程退出后如何让子进程终止?