help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] A do nothing function


From: Bob Proulx
Subject: Re: [Help-bash] A do nothing function
Date: Fri, 28 Sep 2012 14:12:08 -0600
User-agent: Mutt/1.5.21 (2010-09-15)

DJ Mills wrote:
> Greg Wooledge wrote:
> > Bob Proulx wrote:
> >>   filter=cat
> >>   if [ -f tun0.net ]; then
> >>     filter="sed /^eth/d;/^wlan/d;/^NetworkManager/d"
> >>   fi
> >>   RSLVCNFFILES="$(/lib/resolvconf/list-records | $filter | sed -e 
> >> '/^lo$/d' -e '/^lo[.]/d')"
> >
> > Using a function would be more flexible, and less likely to break:
> > filter() { cat; }
> > if [ -f tun0.net ]; then
> >   filter() { sed /^eth/d;/^wlan/d;/^NetworkManager/d; }

Note missing quoting needed in the sed argument.

> > fi
> > RSLVCNFFILES="$(/lib/resolvconf/list-records | filter | ...)"

What would be likely to break?  It seems like six of one and a half
dozen of the other...

Quoting is arguably simpler.  But given the simple case here it
doesn't really matter.  But in a more complex situation I think that
quoting complexity reduction would be a winning point.

> I think I'd put the condition in the function:
> filter() {
>   if [ -f tun0.net ]; then
>     sed '/^eth/d; /^wlan:/d; /^NetworkManager/d'
>   else
>     cat
>   fi
> }
> 
> blah=$(foo | filter | ....)

A matter of taste.  Since these stanzas are each executed exactly once
only there isn't much to sway one way or the other.

But if it were executed many times then I would prefer to do the stat
file test once only and set the variable and then use the value
repeatedly.  Putting things in the function would normally be coupled
with calling the function many times.  But putting the test in the
function and calling the function repeatedly as above with the file
stat in the test would create unneeded stat calls.

So far I still prefer the simple variable expansion over the function
due to it being the simpler method with no functions needed at all.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]