bourne shell query

Roger Rohrbach roger at kymijoki.wrs.com
Sat Sep 1 07:37:32 AEST 1990


fred at maccs.dcss.mcmaster.ca (Fred Whiteside) writes:

>cd /usr/spool/news/comp/std/c
>zots=$1
>echo "Should ignore files with name < $zots"
>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
>echo $files


There are two problems: first, awk expects its first argument to be a
an awk program; you are giving the program as 4 separate arguments
($1, '>=', $zots, {print}), so your program currently will cause awk
to bail out.  Second, you are not escaping the "$1" in your awk program,
so the shell is substituting the first parameter to your script there
(i.e., awk is seeing the number you gave as an argument, not "$1").
If you change the last part of your pipeline to

    awk '$1 >= '$zots' {print}'

it will work, because the "$1" is quoted for awk's sake, and all white
space is quoted to prevent the shell from passing 4 strings, not 1, to
awk as arguments.  Note that we step out of the quotes long enough to
let the shell substitute for "$zots".


ADVANCED DISCUSSION:

One might think you could use double quotes around the whole awk program,
and just escape the "$1" but not "$zots", thus:

    awk "\$1 >= $zots {print}"

and indeed this would work if it were not for the fact that the entire
pipeline is enclosed in backquotes.  A peculiarity of backquoting is
that it forces an extra level of evaluation, so "\$1" is no safer than
"$1".  You must use

    awk "\\$1 >= $zots {print}"

to get this to work.

Another subtlety: if your program were not doing a numeric comparison,
you'd have to arrange for "$zots" to be presented to awk as a string.
My first example would become something like

    awk '$1 == "'$zots'" {print}'

and the second,

    awk "\\$1 == \"$zots\" {print}"

Roger Rohrbach                                  sun!wrs!roger    roger at wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "I was an infinitely hot and dense dot."                            |



More information about the Comp.unix.shell mailing list