BTS

Message1887

Author ft
Recipients maddi, mika
Date 2009-02-14.21:32:22
Content
Michael Prokop <bts@bts.grml.org>:
> * Frank Terbeck <bts@bts.grml.org> [20090214 21:59]:
> 
> > > if type -a "$PROG" 1>/dev/null 2>&1 ; then
> 
> > > => Is this really a bashism?
> 
> > 'type -a' is probably. I don't even know what it does.
> > So, bash it is.
> 
> Just using 'which $PROG" would fix this issue, right? :)

Well, I don't think 'which' is covered by SUSv3 either. I think
originally, that was a csh script. If you really want POSIX
conformance, you'd do a loop over the dirs in $PATH and check if the
program in question is there and executable. That could go into a
shell library and would actually be cleaner than the
which-and-forget-about-the-output approach.

Such a function would look like this:

[snip]
is_installed() {
    prog="$1"

    ret=1
    oifs="$IFS"
    IFS=:
    for dir in $PATH; do
        [ -x "${dir:-.}/$1" ] && ret=0
    done

    IFS="$oifs"
    return "$ret"
}
[snap]

'which' isn't a builtin in shells like posh, dash or even bash.
Those rely on /bin/which or similar. And of course, an additional
fork() screws performance. :-)

Regards, Frank
History
Date User Action Args
2009-02-14 21:32:23ftsetrecipients: + mika
2009-02-14 21:32:23ftlinkissue431 messages
2009-02-14 21:32:22ftcreate