Reputation: 3
I'm attempting to execute diff from within a awk script thusly...
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...
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...
/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).
I've tried variations with single quotes to no avail as well, does anyone have any idea where my shell-escape-foo is lacking?
Upvotes: 0
Views: 902
Reputation: 75548
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.
cmd="shopt +o posix\ndiff <(echo \""bServers[nonServers]"\") <(echo \""primeReference"\")"
Or explicitly call 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.
Upvotes: 2