以下命令的输出
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没有被执行。
答案 0 :(得分:1)
你必须逃避" $"符号,为了不在本地获取,而是远程传递。
$ 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