Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
From: peter@abbnm.com (Peter da Silva)
Subject: Re: Unexpected result from grep
Date: Fri, 19 Dec 2003 20:11:50 +0000 (UTC)	[thread overview]
Message-ID: <brvm26$ef0$1@jeeves.eng.abbnm.com> (raw)
In-Reply-To: DD2Cb.62750$%h4.33767@twister.tampabay.rr.com

My biennial lament about excessive use of non-portable shell syntax:

In article <DD2Cb.62750$%h4.33767@twister.tampabay.rr.com>,
Joseph Dionne  <no@emailplease.org> 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`


  parent reply	other threads:[~2003-12-19 20:11 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-11 16:13 Unexpected result from grep Dan
2003-12-11 16:32 ` Jens Schweikhardt
2003-12-11 16:37 ` Chris F.A. Johnson
2003-12-11 16:47 ` Stephane CHAZELAS
2003-12-11 18:24   ` Joseph Dionne
2003-12-12 13:19     ` Dan
2003-12-19 20:11     ` Peter da Silva [this message]
2003-12-20 16:13       ` Dan Mercer
2003-12-22 21:14         ` Peter da Silva
2003-12-23 16:01           ` Dan Mercer
2003-12-23 21:01             ` Peter da Silva
2003-12-23 22:15               ` Dan Mercer
2003-12-25 22:29                 ` Peter da Silva
2003-12-24 16:41       ` Stephane CHAZELAS
2003-12-25 22:15         ` Peter da Silva
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox