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

Re: please stop hardcoding paths to programs



On Thu, Apr 29, 2004 at 02:15:33PM -0400, Joey Hess wrote:
> The only acceptable time to hardcode a path is a test -x to see if a
> program exists (busybox does not have a which, so that can't be used).
> Please think very carefully before hardcoding a path even there and try
> to find an alternative.

If that's necessary, this shell function (which I use in
base-passwd.postinst) should do the job. I haven't tested it in busybox,
but it isn't complex shell.

searchpath () {
        PROGRAM="$1"
        IFS_SAVE="$IFS"
        IFS=:
        RET=1
        for ELEMENT in $PATH; do
                if [ -z "$ELEMENT" ]; then
                        ELEMENT=.
                fi
                if [ -f "$ELEMENT/$PROGRAM" ] && \
                   [ -x "$ELEMENT/$PROGRAM" ]; then
                        RET=0
                        break
                fi
        done
        IFS="$IFS_SAVE"
        return "$RET"
}

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]



Reply to: