Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 30 Aug 2009 16:18:45 -0700
From:      Bill Campbell <freebsd@celestial.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: shell command line argument + parsing function
Message-ID:  <20090830231845.GA991@ayn.mi.celestial.com>
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, 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.

Did you put $@ in quotes?  That is parse_cmdline "$@".  I haven't
tried this in calling functions in scripts, but use it frequently
when calling scripts from other scripts.  I would probably use
something like:

for arg in "$@"; do
	dosomething "$arg"
done

Bill
-- 
INTERNET:   bill@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Good luck to all you optimists out there who think Microsoft can deliver
35 million lines of quality code on which you can operate your business.
   -- John C. Dvorak



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