Hi all,

These are just a couple of thoughts I've had that we've discussed a
little in IRC recently. They aren't exactly earth-shattering, but I
think they might still be of use to someone.

Shell aliases which give default arguments, like alias ls='ls -1', are
useful but must be invoked from the shell, which means they aren't
'first class citizens' as programs in Unix. The solution, then, is to
have small scripts in ~/bin which wrap the program, like ~/bin/ls:

    #!/bin/sh
    exec /bin/ls -1 "$@"

The problem with this is that now we've circumvented $PATH by saying
'exec /bin/ls', an absolute path, rather than simply 'ls'. But if we
did say 'exec ls' then it would infinite loop, with ~/bin/ls executing
~/bin/ls... Our solution was 'next', a shell script which works out
the executable in $PATH to execute *after* the current one. So instead
you would say:

    #!/bin/sh
    exec next "$0" -1 "$@"

This has proven quite convenient. The script is as follows:

    #!/bin/sh

    dir=${1%/*}
    bin=${1##*/}
    shift

    unset flag
    set -f
    IFS=:

    for d in $PATH; do
    if [ "$d" = "$dir" ]; then
        flag=1
    elif [ -n "$flag" ] && [ -x "$d/$bin" ]; then
        exec "$d/$bin" "$@"
    fi
    done

    printf "%s: no '%s' found beyond '%s' in \$PATH\n" "${0##*/}"
"$bin" "$dir" >&2
    exit 127

Thanks,
cls

Reply via email to