Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 31 Aug 2009 01:36:41 +0200
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        Stefan Miklosovic <miklosovic.freebsd@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: shell command line argument + parsing function
Message-ID:  <20090830233641.GA48910@owl.midgard.homeip.net>
In-Reply-To: <f99a79ec0908301607l7772a486j1986b87d31d33cef@mail.gmail.com>
References:  <f99a79ec0908301607l7772a486j1986b87d31d33cef@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090830233641.GA48910>