shell command line argument + parsing function

Stefan Miklosovic miklosovic.freebsd at gmail.com
Sun Aug 30 23:07:38 UTC 2009


hi,

assuming I execute shell script like this

$ ./script -c "hello world"

I want to save "hello world" string to variable COMMENT in shell script.

code:

#!/bin/sh

parse_cmdline() {
    while [ $# -gt 0 ]; do
        case "$1" in
            -c)
                shift
                COMMENT="$1"
                ;;
        esac
        shift
    done
}

parse_cmdline $*

echo $COMMENT

exit 0

but that only write out "hello". I tried to change $* to $@, nothing
changed.

It is interesting, that if I dont put "while" loop into function
parse_cmdline,
and do echo $COMMENT, it writes "hello world".

I WANT that function style. How to do it ?

thank you


More information about the freebsd-questions mailing list