以下命令的输出
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/