Article View: comp.unix.shell
Article #982Re: bourne shell query
From: Roger Rohrbach
Date: Fri, 31 Aug 1990 21:37
Date: Fri, 31 Aug 1990 21:37
55 lines
2494 bytes
2494 bytes
fred@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@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
| {o >o |
| \ -) "I was an infinitely hot and dense dot." |
Technical Details
Message-ID:
<1209@wrs.wrs.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!mips!pacbell.com!pacbell!rtech!wrs!kymijoki!rogerReferences:
<26DC6447.15922@maccs.dcss.mcmaster.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!mips!pacbell.com!pacbell!rtech!wrs!kymijoki!roger From: roger@kymijoki.wrs.com (Roger Rohrbach) Newsgroups: comp.unix.shell Subject: Re: bourne shell query Message-ID: <1209@wrs.wrs.com> Date: 31 Aug 90 21:37:32 GMT References: <26DC6447.15922@maccs.dcss.mcmaster.ca> Sender: news@wrs.wrs.com Lines: 55