转义 awk 命令
Escape awk command
我有一个 unix(aix) 命令,其中包含一个小的 awk 脚本。它有效,在这里...
ps -eaf | awk 'ARGIND == 1 {$pids[[=10=]] = 1} ARGIND > 1 {if ( in pids) printf("%s\n",[=10=])}' /home/richard/myFile.flg -
当我 运行 使用 ssh 从不同的盒子执行此命令时,它不起作用。
ssh myuser@myOtherBox ps -eaf | awk 'ARGIND == 1 {$pids[[=11=]] = 1} ARGIND > 1 {if ( in pids) printf("%s\n",[=11=])}' /home/richard/myFile.flg -
我发现我需要引用 awk 脚本并转义 awk 命令中的一些字符,但我无法正确转义。
有人能帮我引用脚本的 awk 部分并转义所需的内容吗?
谢谢
执行时会发生什么
ssh myuser@myOtherBox ps -eaf | ...
是ps -eaf
是另一个盒子上的运行,输出返回; ssh
然后将它接收到的输出写入它自己的标准输出,它通过命令 ...
(本地)重定向;在这种情况下,一个 awk 命令。
不幸的是,(我假设)/home/richard/myFile.flg
在远程 mache 而不是本地机器上,所以 awk 命令失败。
要将整个内容发送到远程计算机上的 运行,您需要将其作为单个参数提供;一种不需要太多引用工作的方法是使用此处文档:
ssh myuser@myOtherBox "$(cat<<"END"
ps -eaf |
awk 'ARGIND == 1 {pids[[=11=]] = 1}
ARGIND > 1 {if ( in pids) printf("%s\n",[=11=])}' \
/home/richard/myFile.flg -
END
)"
请注意 printf("%s\n",[=18=])
实际上只是 print
的一种复杂写法,因此您可以大大简化远程命令。但是您仍然需要处理 awk 命令中的单引号:
ssh myuser@myOtherBox '
ps -eaf |
awk '"'"'ARGIND == 1 {pids[[=12=]] = 1; next}
in pids {print}'"'"' \
/home/richard/myFile.flg -'
要理解'"'"'
,你需要把它拆解:
' close '-quoted string
"'" A (quoted) '
' open another '-quoted string
在这种情况下你需要双重转义,这应该有效:
ssh myuser@myOtherBox "ps -eaf | awk \"ARGIND == 1 {\$pids[\$0] = 1} ARGIND > 1 {if (\$2 in pids) printf(\\"%s\n\\",\$0)}\" /home/richard/myFile.flg -"
如果你能使用bash的$'STRING'语法,那么事情就很简单了
可读;在这种情况下,只需要转义单引号和
反斜杠:
$'ps -eaf |
awk \'
ARGIND == 1 {$pids[[=10=]] = 1}
ARGIND > 1 {if ( in pids) printf("%s\n",[=10=])}\' /home/richard/myFile.flg -'
我有一个 unix(aix) 命令,其中包含一个小的 awk 脚本。它有效,在这里...
ps -eaf | awk 'ARGIND == 1 {$pids[[=10=]] = 1} ARGIND > 1 {if ( in pids) printf("%s\n",[=10=])}' /home/richard/myFile.flg -
当我 运行 使用 ssh 从不同的盒子执行此命令时,它不起作用。
ssh myuser@myOtherBox ps -eaf | awk 'ARGIND == 1 {$pids[[=11=]] = 1} ARGIND > 1 {if ( in pids) printf("%s\n",[=11=])}' /home/richard/myFile.flg -
我发现我需要引用 awk 脚本并转义 awk 命令中的一些字符,但我无法正确转义。
有人能帮我引用脚本的 awk 部分并转义所需的内容吗?
谢谢
执行时会发生什么
ssh myuser@myOtherBox ps -eaf | ...
是ps -eaf
是另一个盒子上的运行,输出返回; ssh
然后将它接收到的输出写入它自己的标准输出,它通过命令 ...
(本地)重定向;在这种情况下,一个 awk 命令。
不幸的是,(我假设)/home/richard/myFile.flg
在远程 mache 而不是本地机器上,所以 awk 命令失败。
要将整个内容发送到远程计算机上的 运行,您需要将其作为单个参数提供;一种不需要太多引用工作的方法是使用此处文档:
ssh myuser@myOtherBox "$(cat<<"END"
ps -eaf |
awk 'ARGIND == 1 {pids[[=11=]] = 1}
ARGIND > 1 {if ( in pids) printf("%s\n",[=11=])}' \
/home/richard/myFile.flg -
END
)"
请注意 printf("%s\n",[=18=])
实际上只是 print
的一种复杂写法,因此您可以大大简化远程命令。但是您仍然需要处理 awk 命令中的单引号:
ssh myuser@myOtherBox '
ps -eaf |
awk '"'"'ARGIND == 1 {pids[[=12=]] = 1; next}
in pids {print}'"'"' \
/home/richard/myFile.flg -'
要理解'"'"'
,你需要把它拆解:
' close '-quoted string
"'" A (quoted) '
' open another '-quoted string
在这种情况下你需要双重转义,这应该有效:
ssh myuser@myOtherBox "ps -eaf | awk \"ARGIND == 1 {\$pids[\$0] = 1} ARGIND > 1 {if (\$2 in pids) printf(\\"%s\n\\",\$0)}\" /home/richard/myFile.flg -"
如果你能使用bash的$'STRING'语法,那么事情就很简单了 可读;在这种情况下,只需要转义单引号和 反斜杠:
$'ps -eaf |
awk \'
ARGIND == 1 {$pids[[=10=]] = 1}
ARGIND > 1 {if ( in pids) printf("%s\n",[=10=])}\' /home/richard/myFile.flg -'