break while loop in BASH

Hi gurus, I have the following part of code which I am using for treating input

#!/bin/bash

while [[ $1 = -* ]]; do
    arg=$1; shift

    case $arg in
        -u)
            users="$1"
            shift
            ;;
        -g)
            groups="$1"
            shift
            ;;
        -m)
            mlength="$1"
            shift
            ;;
        -t)
            dfileg="$1"
            shift
            ;;
        --help)
            echo "-m maximum count of characters per line in group file"
            echo "for errors see file called: errorlog"
            break
            ;;
        *)
            break
            ;;

when I issue

./script -u john -g daemon -m 100 -t group

everything works fine

but when I issue

./script --help

the script echos text but did not exit. (I have to interrupt it by ctrl+c) how can I close script after issuing --help ? Thanks a lot

Change 'break' to 'exit' in the --help case.

Thanks a lot, this helps :wink: BTW is there any more elegant way of treating input ?

Hi wakatana:

I don't think I would go so far as to call it elegant, but there is a standardized utility for dealing with command line options and arguments. getopts

It's usually implemented as a shell built-in, so search your shell's man page for 'getopts'.

Regards,
Alister

yes, you should use getopts