From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1012a6,f7f68e18cfb8d31b X-Google-Attributes: gid1012a6,public X-Google-Thread: fa528,f7f68e18cfb8d31b X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2003-12-19 12:23:27 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!news.gtei.net!newsfeed1.easynews.com!easynews.com!easynews!cyclone1.gnilink.net!canoe.uoregon.edu!hammer.uoregon.edu!logbridge.uoregon.edu!newsfeed.stanford.edu!news-proxy.abbnm.com!newsboy.eng.abbnm.com!not-for-mail From: peter@abbnm.com (Peter da Silva) Newsgroups: comp.unix.shell,comp.unix.tru64 Subject: Re: Unexpected result from grep Date: Fri, 19 Dec 2003 20:11:50 +0000 (UTC) Organization: ABB Inc., Network Management Division Message-ID: References: NNTP-Posting-Host: grendel.eng.abbnm.com X-Trace: jeeves.eng.abbnm.com 1071864710 14816 198.178.0.150 (19 Dec 2003 20:11:50 GMT) X-Complaints-To: usenet@jeeves.eng.abbnm.com NNTP-Posting-Date: Fri, 19 Dec 2003 20:11:50 +0000 (UTC) X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Originator: peter@abbnm.com (Peter da Silva) Xref: archiver1.google.com comp.unix.shell:3650 comp.unix.tru64:11044 My biennial lament about excessive use of non-portable shell syntax: In article , Joseph Dionne wrote: > I have noticed that the Tru64 version of ksh is not a true Korn shell. Doesn't matter if it is or not, {grep \'$var\' ..} is not going to produce the intended result. It looks like the result of copying something from a vaguely similar-looking script that was doing something completely different, rather than actually working out what you need to do. The whole script is excessively complex. Even assuming you want to parameterise the grep command, this would do precisely the same thing: COMMAND=/sbin/grep RESULT="`"$COMMAND" -E "$1" "$2" 2>/dev/null`" And it would work in any bourne-based shell, and unless typeset is doing something particularly clever with the following line, it avoids a potential security problem related to the re-evaluation of the second argument. > >>typeset -r CHECK_FILE="${2}" As would any of following, of course, but they're not as portable: > > RESULT=$( > > "${EGREP[@]}" -- "$EXPRESSION" "$CHECK_FILE" 2> /dev/null > > ) > > > > or > > > > COMMAND='/sbin/grep -E -- "$EXPRESSION" "$CHECK_FILE" 2> /dev/null' > > RESULT=$(eval "$COMMAND") > > > > GREP=/sbin/grep > > RESULT=$( > > "$GREP" -E -- "$EXPRESSION" "$CHECK_FILE" 2> /dev/null > > ) Basically, when I get an installer or other script that starts with something like #!/bin/ksh or #!/bin/bash I find it falls into one of these categories: 1. It actually needs some functionality that isn't available in generic sh. - 1% 2. It is actually portable "sh" code and just needs the "#!" line fixed. - 9% 3. It uses post-sh functionality, but doesn't actually need it. - 90% I wish people would pay attention to code portability in shell scripts. It's not that hard, and it does pay off. -- I've seen things you people can't imagine. Chimneysweeps on fire over the roofs of London. I've watched kite-strings glitter in the sun at Hyde Park Gate. All these things will be lost in time, like chalk-paintings in the rain. `-_-' Time for your nap. | Peter da Silva | Har du kramat din varg, idag? 'U`