From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,f7f68e18cfb8d31b X-Google-Attributes: gidfa528,public X-Google-Thread: 1012a6,f7f68e18cfb8d31b X-Google-Attributes: gid1012a6,public X-Google-ArrivalTime: 2003-12-20 08:12:24 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!newsfeed.earthlink.net!cyclone.socal.rr.com!cyclone2.kc.rr.com!news2.kc.rr.com!twister.rdc-kc.rr.com.POSTED!53ab2750!not-for-mail From: "Dan Mercer" Newsgroups: comp.unix.shell,comp.unix.tru64 References: Subject: Re: Unexpected result from grep X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Message-ID: <0z_Eb.170955$Eq1.160079@twister.rdc-kc.rr.com> Date: Sat, 20 Dec 2003 16:13:48 GMT NNTP-Posting-Host: 24.163.211.18 X-Complaints-To: abuse@rr.com X-Trace: twister.rdc-kc.rr.com 1071936828 24.163.211.18 (Sat, 20 Dec 2003 10:13:48 CST) NNTP-Posting-Date: Sat, 20 Dec 2003 10:13:48 CST Xref: archiver1.google.com comp.unix.shell:3678 comp.unix.tru64:11045 "Peter da Silva" wrote in message news:brvm26$ef0$1@jeeves.eng.abbnm.com... : 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. Portability using a minimal sh functionality pushes the burden of portability from the shell to the OS. You then must rely the portability of shell utilities like awk (which may actualy be old awk) or perl (which may not be installed or may be perl 4) or sed or find (gnu find has some significant incompatibilities with SysV find). Even the shell known as "sh" may have some significant incompatibilities - it may be influenced by the ENV variable (many "posix" shells), it may source ~/.shrc (NCR SysVR3 sh) - it may or may not accept options to echo. It is much easier to code for portable ksh - or, failing that, provide your own shell like Oracle does with osh. Even the location of sh is problematic - HP-UX puts it in /usr/bin and symbolically links /usr/bin to /bin - but calls that a "transitional link" indicating that users should prepare for a time when /bin does not exist at all. It's too bad the POSIX shell group did such a poor job - too much infighting from people with vested interests. Personally, I would have been perfectly happy had they just said : "We're going to use zsh - get over it." Dan Mercer : : -- : 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`