awk coprocess包含括号的转义序列

时间:2021-05-26 16:04:04

I'm attempting to execute diff from within a awk script thusly...

我试图在awk脚本中执行diff ...

cmd="diff <(echo \""bServers[nonServers]"\") <(echo \""primeReference"\")"
print cmd
while (( cmd | getline result) > 0 ) {print result}
close(cmd)}

...where bServers[nonServers] and primeReference evaluate as vanilla strings with no special characters. Hence the "print cmd" produces...

...其中bServers [nonServers]和primeReference评估为没有特殊字符的vanilla字符串。因此“print cmd”会产生......

diff <(echo "
> /var/tmp/text1.txt 818e9c0fc3dd92e86c80704419b1cc0d") <(echo "
> /var/tmp/text2.txt 00efcc10b376dbdd6d0972635eb650c4")
2c2
< /var/tmp/text1.txt 818e9c0fc3dd92e86c80704419b1cc0d
---
> /var/tmp/text2.txt 00efcc10b376dbdd6d0972635eb650c4

...which works fine when cut and paste to the command line. but when run as part of the Awk command returns...

...剪切并粘贴到命令行时工作正常。但是当作为Awk命令的一部分运行时返回...

/var/tmp/text2.txt 818e9c0fc3dd92e86c80704419b1cc0d")
sh: -c: line 0: syntax error near unexpected token `('
'h: -c: line 0: `diff <(echo "

A single escaping backslash before the open bracket produces...

打开支架前的单个转义反斜杠产生......

awk: warning: escape sequence `\(' treated as plain `('

Double produces...

双产......

sh: (echo: No such file or directory

Triple backslashes produces...

三个反斜杠产生......

awk: warning: escape sequence `\(' treated as plain `('
...
sh: (echo: No such file or directory

...and four backslashes (for laughs) just repeats the cycle with an ever increasing number of backslashes ad infinitum.

......和四个反斜杠(笑声)只是无限地反复循环,不断增加反斜率。

The shell is GNU bash version 4.1.2(1)-release, awk is GNU Awk 3.1.7 and OS is CentOS release 6.2 (Final).

shell是GNU bash版本4.1.2(1)-release,awk是GNU Awk 3.1.7,OS是CentOS版本6.2(最终版)。

I've tried variations with single quotes to no avail as well, does anyone have any idea where my shell-escape-foo is lacking?

我尝试使用单引号的变体也没有用,有没有人知道我的shell-escape-foo缺少哪里?

1 个解决方案

#1


1  

sh is the default shell in awk and not bash so it's better if you just use bash as a whole if possible. Bash also runs in POSIX mode if ran as sh and doesn't recognize process substitution (<(..)). However you can try disabling POSIX mode if sh is actually a link to bash e.g.

sh是awk中的默认shell而不是bash所以如果可能的话,你最好只使用bash作为一个整体。如果以sh运行并且不识别进程替换(<(..)),Bash也在POSIX模式下运行。但是,如果sh实际上是bash的链接,则可以尝试禁用POSIX模式,例如

cmd="shopt +o posix\ndiff <(echo \""bServers[nonServers]"\") <(echo \""primeReference"\")"

Or explicitly call bash:

或明确调用bash:

cmd="bash -c \"exec diff <(echo '"bServers[nonServers]"') <(echo '"primeReference"')\""

The values bServers[nonServers] and primeReference could still affect syntax though. This is the reason why using bash as a whole would be preferrable most of the time when reading output of external commands is needed.

值bServers [nonServers]和primeReference仍然会影响语法。这就是为什么在需要读取外部命令输出时大多数时候使用bash作为一个整体的优选原因。

#1


1  

sh is the default shell in awk and not bash so it's better if you just use bash as a whole if possible. Bash also runs in POSIX mode if ran as sh and doesn't recognize process substitution (<(..)). However you can try disabling POSIX mode if sh is actually a link to bash e.g.

sh是awk中的默认shell而不是bash所以如果可能的话,你最好只使用bash作为一个整体。如果以sh运行并且不识别进程替换(<(..)),Bash也在POSIX模式下运行。但是,如果sh实际上是bash的链接,则可以尝试禁用POSIX模式,例如

cmd="shopt +o posix\ndiff <(echo \""bServers[nonServers]"\") <(echo \""primeReference"\")"

Or explicitly call bash:

或明确调用bash:

cmd="bash -c \"exec diff <(echo '"bServers[nonServers]"') <(echo '"primeReference"')\""

The values bServers[nonServers] and primeReference could still affect syntax though. This is the reason why using bash as a whole would be preferrable most of the time when reading output of external commands is needed.

值bServers [nonServers]和primeReference仍然会影响语法。这就是为什么在需要读取外部命令输出时大多数时候使用bash作为一个整体的优选原因。