shell command line argument + parsing function

Erik Trulsson ertr1013 at student.uu.se
Sun Aug 30 23:36:54 UTC 2009


On Mon, Aug 31, 2009 at 01:07:36AM +0200, Stefan Miklosovic wrote:
> 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.

But if you use "$@" (with the quote marks) instead it should work fine.
For further explanation please read the sh(1) man page where it explains the
special parameters $* and $@, while paying special attention to how they
expand when used within double-quotes.


> 
> 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
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"

-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013 at student.uu.se


More information about the freebsd-questions mailing list