0

I'm writing a script that involves generating Awk programs and running them via awk $(...), as in

[lynko@hephaestus] ~ % awk $(echo 'BEGIN { print "hello!" }')

The generated program is going to be more complicated in the end, but first I want to make sure this is possible. In the past I've done

[lynko@hephaestus] ~ % program=$(echo 'BEGIN { print "hello" }')
[lynko@hephaestus] ~ % awk "$program"
hello!

where the grouping is unsurprising. But the first example (under GNU awk, which gives a more helpful error message than mawk which is default on my other machine) gives

[lynko@hephaestus] ~ % awk $(echo 'BEGIN { print "hello!" }')
awk: cmd. line:1: BEGIN blocks must have an action part

presumably because this is executed as awk BEGIN { print "hello!" } rather than awk 'BEGIN { print "hello!" }'. Is there a way I can force $(...) to remain as one group? I'd rather not use "$()" since I'd have to escape all the double-quotes in the program generator.

I'm running Bash 4.2.37 and mawk 1.3.3 on Crunchbang Waldorf.

4

2 回答 2

1

我也想知道你为什么使用 echo 语句。awk 不需要。

awk 'BEGIN { print "Awk SQUAWK!" }'

这将完美地工作。

于 2014-07-20T12:18:10.657 回答
1

Put quotes around it. You don't need to escape the double quotes inside it:

awk "$(echo 'BEGIN { print "hello!" }')"
于 2014-07-20T09:04:48.897 回答