[dev] Odds and ends (1/2): next.sh

From: Connor Lane Smith <cls_AT_lubutu.com>
Date: Tue, 21 Apr 2015 20:16:02 +0100

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:

    exec /bin/ls -1 "$_AT_"

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:

    exec next "$0" -1 "$_AT_"

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



    unset flag
    set -f

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

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

Received on Tue Apr 21 2015 - 21:16:02 CEST

This archive was generated by hypermail 2.3.0 : Tue Apr 21 2015 - 21:24:08 CEST