Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!zephyr.ens.tek.com!orca.wv.tek.com!quark!jeff From: jeff@quark.WV.TEK.COM (Jeff Beadles) Newsgroups: comp.unix.shell Subject: Re: bourne shell query Message-ID: <8446@orca.wv.tek.com> Date: 30 Aug 90 14:55:44 GMT References: <26DC6447.15922@maccs.dcss.mcmaster.ca> Sender: news@orca.wv.tek.com Reply-To: jeff@quark.WV.TEK.COM (Jeff Beadles) Organization: Tektronix, Inc., Wilsonville, OR Lines: 73 fred@maccs.dcss.mcmaster.ca (Fred Whiteside) writes: :> :> i have a simple little shell question. I am attempting :>to write a script that will perform various manipulations on news :>articles. my problem seems to be with understanding variable :>substitution in sh. a minimal example is the enclosed sh script. what :>it should do is set the variable files to be the names of those :>articles in comp.std.c whose name (article number) is greater than or :>equal to the numeric value of the scripts first argument. this does :>not behave as expected. i have tried quite a unmber of variations on :>this theme (mostly centering around multiple $'s and interesting :>quoting of commands, but i tend to get all of the files or none of them :>depending on (for some reason) the number of $'s preceding the zots :>variable name (also on the number in front of the 1, $1 or $$1, etc. :>but i expected that ...) this is running under sunos4.1 if that is :>interesting. i have tried to read the manual, but with little :>success. anyone interested in pointing out my clear stupidity :>publicly? i *would* appreciate it .... :> :> many thanks ... :> :>#!/bin/sh :>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}` I see a quoting problem here ^^^^^^^^^^^^^^^^^^^^^ :>echo $files :> :>-- :>Fred Whiteside Chedoke-McMaster Hospitals, Hamilton, Ontario, Canada :>whitesid@{mcmaster.ca,darwin.cmh.mcmaster.ca,130.113.2.18} :>fred@maccs.DCSS.McMaster.CA ...!uunet!utai!utgpu!maccs!fred >files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}` This line is the problem, replace it with: files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk "\$1 >= $zots {print}"` Or, to make it a little more simpler... (You don't need .*'s in the grep) files=`ls -rt * |grep -v '[a-zA-Z]' | awk "\$1 >= $zots {print}"` To explain: The only real change was to change thw quoting around the awk script. $1 was getting expanded to the shell's $1, not the 1st awk field. By using " as a quoting character, the shell is able to expand $zot to whatever number it represents. The \$1 is to prevent the shell from trying to expand $1 to the shell's $1, rather than the 1st awk field. After the shell is finished with variable substitution, awk "\$1 >= $zots {print}" is transformed, and awk see's: (if zot = 10) awk "$1 >= 10 {print}" Hope this helps! A decent book on shell programming and quoting is the Howard Sam's book on shell programming. -Jeff -- Jeff Beadles jeff@quark.WV.TEK.COM Utek Engineering, Tektronix Inc. +1 503 685 2568 SPEEA - Just say no.