문제

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 if 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top