bug-bash
[Top][All Lists]
Advanced

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

Re: test for "command not found" before expanding shell parameters


From: Dave Finlay
Subject: Re: test for "command not found" before expanding shell parameters
Date: Wed, 23 Apr 2014 00:02:26 -0700

This might suffice for the initial situation, as long as the expansion is
passed in with single quotes:

function check_n_run() {
    greo="$1"; wild="$2"
    if $(which "$greo" > /dev/null); then
        $greo $wild;
    fi
}

This is too cute to be useful, but it was enjoyable seeing the unintended
consequences when replace other, more used commands.

function greo() {
    wild="$@"
    if $(which "${FUNCNAME[0]}" > /dev/null); then
        $(which "${FUNCNAME[0]}") $wild
    fi
}



Dave Finlay


On Tue, Apr 22, 2014 at 1:03 PM, Chet Ramey <chet.ramey@case.edu> wrote:

> > On Mon, Apr 21, 2014 at 3:16 AM, Andreas Schwab <schwab@linux-m68k.org>
> wrote:
> > > And if $greo is null the condition will also be true.
> >
> > Really? <runs quick test> I'll be damned.  That explains this problem
> > I've been having.
>
> Yes.  `test' operates based on the number of arguments it receives.  If you
> don't quote `$greo' and it expands to nothing, test receives a single
> argument (-n), tests it for nullness, and returns success.
>
> Chet
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRU    chet@case.edu
> http://cnswww.cns.cwru.edu/~chet/
>
>


reply via email to

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