rfinch@caldwr.water.ca.gov (Ralph Finch) (12/14/90)
I do not understand why the following bourne shell script generates an
error in the awk program:
(call the script file test1)
#!/bin/sh
filename=$1 ; shift
nargs=$#
args="$*"
echo $args
gawk '
BEGIN {
args="'$args'"
print args
exit
}' $filename
(running the script file):
test1 abc 1 2 3
1 2 3
gawk: syntax error near line 3:
args="1
^ unterminated string
--
Ralph Finch 916-445-0088
rfinch@water.ca.gov ...ucbvax!ucdavis!caldwr!rfinch
Any opinions expressed are my own; they do not represent the DWR
mcgrew@ichthous.Eng.Sun.COM (Darin McGrew) (12/14/90)
rfinch@caldwr.water.ca.gov (Ralph Finch) writes: >I do not understand why the following bourne shell script generates an >error in the awk program: The whitespace in $args is not being quoted, which causes the gawk program argument to be broken up into several arguments. The following should work better-- #!/bin/sh filename=$1 ; shift nargs=$# args="$*" echo $args gawk ' BEGIN { args="'"$args"'" print args exit }' $filename Darin McGrew mcgrew@Eng.Sun.COM Affiliation stated for identification purposes only.
karish@mindcraft.com (Chuck Karish) (12/14/90)
In article <247@locke.water.ca.gov> rfinch@caldwr.water.ca.gov (Ralph Finch) writes: >I do not understand why the following bourne shell script generates an >error in the awk program: > >filename=$1 ; shift >nargs=$# >args="$*" >echo $args > >gawk ' >BEGIN { >args="'$args'" >print args >exit >}' $filename The shell argument on line 3 of the awk program ($args) is outside the single quotes (as it must be, to be interpreted by the shell rather than by awk). It must be interpreted as a single token by the shell if the program is to be passed to awk properly. What happens is that the shell sees the spaces between the args in $args and sends $# - 1 arguments to awk when it should always send two, the program and the filename. If you want to check this out, replace 'gawk' in the script with the name of a script that reads: #! /bin/sh echo $# Fix the problem by adding double quotes: args="'"$args"'" The inner double quotes are interpreted by the shell to group $args into a single variable. The single quotes protect the rest of the awk program from interpretation by the shell. The outer double quotes tell awk that the string produced by the shell on interpreting $args is a literal to be assigned to the awk 'args' variable, rather than a variable name. Another way to solve the problem is to change $IFS between the time you set "$args" and the time you interpret it. Thanks for asking the question. I've known for a long time that spaces in this context cause problems, but had never taken the time to figure out why. -- Chuck Karish karish@mindcraft.com Mindcraft, Inc. (415) 323-9000