This one has me stumped.
#!/bin/ksh
AWKSCRIPT='END { print "all done"; }'
OUTPUT=`echo hello world | awk '$AWKSCRIPT'`
RETVAL=$?
echo "running echo hello world | awk '$AWKSCRIPT'"
echo "Output = $OUTPUT"
echo "returned = $RETVAL"
The output is
$ ./kshawk.ksh
Output = hello world
returned = 0
(I was expecting to see "Output = all done")
It looks like the interpreter is not substituting the AWKSCRIPT variable when evaluating the expression (I get the same behaviour i开发者_StackOverflow中文版f I use $(...) instead of backticks).
While I could dump AWKSCRIPT to a temporary file - this would have to be hardcoded too?
Any ideas how to interpolate a variable within backticks?
The single quotes around '$AWKSCRIPT'
prevent the interpolation of the variable. Double quotes do allow interpolation:
$ OUTPUT=`echo hello world | awk "$AWKSCRIPT"`
$ echo $OUTPUT
all done
精彩评论