Mailinglist Archive: opensuse-factory (883 mails)

< Previous Next >
Re: grep, cut, and all that (Re: [opensuse-factory] Status: Beta)
On 5/29/2012 3:48 PM, Claudio Freire wrote:
On Tue, May 29, 2012 at 4:44 PM, Brian K. White<brian@xxxxxxxxx> wrote:
You don't need awk or anything but the same shell that's already executing
the script.

GetVal () {
DONE=false
until $DONE ; do
IFS=: read KEY VAL junk || DONE=:
[[ "$KEY" == "$1" ]] && { echo $VAL ; DONE=: ; }
done< $2
}

Isn't that bash-specific? Don't spec scripts have to be more generic
(sh-compatible)?


No. That is, this off the cuff example may possibly have some bash-specific syntax, but it does not use or rely on any bash-specific features.

This practically identical version works on the stock sco open server /bin/sh which is much cruder than bash or ksh88 let alone ksh93, and works on current bash too, if we cared. Only Linux has /proc anyways (or at least the same /proc contents formatted the same way) but I guess OBS could target ash/busybox environment which would be linux and have /proc yet not bash.

GetVal () {
DONE=false
until $DONE ; do
IFS=: read KEY VAL junk || DONE=:
[ "$KEY" = "$1" ] && { echo $VAL ; DONE=: ; }
done< $2
}

I don't know if stuff like this is more or less efficient than calling grep/awk/etc, I just know that it doesn't physically require any external util at all, which is a win in certain ways.

If you're scanning many or large files or you need fancy regex matching, then of course the external tools become worth it. Each job really should get it's own evaluation. You can do many of the same jobs with your choice of sed or awk or grep or perl or c or whatever.

I always try to minimize just on general principle. Just because grep/awk/whatever is almost always going to be available because surely some other package will require it, is not enough excuse for me to use it if I don't need it. Suppose awk is broken on a machine one day, maybe an update breaks it or something. Either all my zillion scripts built up over years can all break at the same time, or maybe 5 of them can break and maybe only 2 of those are ones any customer even cares about at the moment. Multiply that times every other potential requirement.

--
bkw
--
To unsubscribe, e-mail: opensuse-factory+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse-factory+owner@xxxxxxxxxxxx

< Previous Next >
Follow Ups