On 4/8/2008 6:39 PM, Kenny McCormack wrote:
> In article <47FBFE15.2010500@[EMAIL PROTECTED]
>,
> Ed Morton <morton@[EMAIL PROTECTED]
> wrote:
> ...
>
>>Yes, it is:
>>
>>$ var="#"
>>$ awk 'BEGIN{ print '"$var"' }'
>>awk: cmd. line:1: BEGIN{ print # }
>>awk: cmd. line:1: ^ syntax error
>>$ awk -v var="$var" 'BEGIN{ print var }'
>>#
>
>
> You make it entirely too easy for me:
>
> sh-3.1$ var="#"
> sh-3.1$ action="print"
> sh-3.1$ gawk 'BEGIN {'$action' "'$var'"}'
> #
> sh-3.1$
$ var="hello
world"
$ awk 'BEGIN{ print "'$var'" }'
awk: BEGIN{ print "hello
awk: ^ unterminated string
$ awk -v var="$var" 'BEGIN{ print var }'
hello
world
> Now, to be fair, I'm not saying there isn't some total corner case that
> involves the variable having the value "rm -rf /;echo foo on you | Mail
> prez@[EMAIL PROTECTED]
" or something like that. I think the shell nuts may
> well come up with something, but I do think that for all normal cases,
> the risk is the same using either the "hopping back and forth" method
> and using the "var=" form.
It's not unusual for a shell variable to contain a newline or some other
character that doesn't work when trying this hopping back and forth
between
shell and awk approach, and then that approach just can't be used at all
if your
awk script is in a file to be invoked with awk -f.
> But I don't really care about any of that; the point is that for
> real-world day-to-day use, this is method is good enough. You are, of
> course, free to disagree with that.
>
OK, I disagree with that. Just use a variable. It works for all cases and
it's
just a few extra charaters of typing.
Ed.


|