我想执行带有长参数的 awk 命令,如下所示:
ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && $1!="Destination" {printf "%-15s %-20s\n", $1, $2}'|sort -f "
但有一些错误:
语法错误 源代码行是 1。错误上下文是 NR!=1 && NF>=6 && >>> != <<< awk: 退出 源代码行是 1。
那么,我该如何解决它呢?
答案1
使用here-docs来绕过所有令人讨厌的子shell引用:
ssh you@host <<-\SSH
awk -f 3<<\AWK /dev/fd/3
awk script
as many lines as you like
"$vars and quotes" are only evaluated by awk
#END
AWK
"$vars and quotes" are only evaluated by remote shell
echo 'single quotes and all'
rest of ssh script
#END
SSH
答案2
在远程命令中使用反斜杠来保护$
和:"
ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && \$1!=\"Destination\" {printf \"%-15s %-20s\n\", \$1, \$2}'|sort -f "