Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: unshift



Hi,

On Mon, Apr 01, 2024 at 06:52:00AM -0700, Ray Andrews wrote:
shift
while [ "$1" ]; do
    if [ a ]; then b; c; continue fi
    if [ d ]; then e; f; continue fi
    if [ g ]; then h; i; continue fi
...
shift
done

... because only one 'if' test will be relevant per loop and the
remaining 'if' tests might even do some bad stuff.  The problem of
course is that if I 'continue', the final 'shift' is missed.

Why not just use 'elif'? :)

shift
while [ "$1" ]; do
    if   [ a ]; then b; c;
    elif [ d ]; then e; f;
    elif [ g ]; then h; i;
    fi
    shift
done

If 'a' matches, the rest of the conditional won't run.

You could even use 'case'

shift
while [ "$1" ]; do
    case $1 in
        a) b; c;;
        d) e; f;;
        g) h; i;;
    esac
    shift
done

The execution terminates on ';;'.

--
Dennis Eriksen




Messages sorted by: Reverse Date, Date, Thread, Author