From owner-freebsd-questions@FreeBSD.ORG Sun Aug 30 23:36:54 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0D46C1065670 for ; Sun, 30 Aug 2009 23:36:54 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp01.sth.basefarm.net (ch-smtp01.sth.basefarm.net [80.76.149.212]) by mx1.freebsd.org (Postfix) with ESMTP id BD6F68FC13 for ; Sun, 30 Aug 2009 23:36:53 +0000 (UTC) Received: from c83-255-48-78.bredband.comhem.se ([83.255.48.78]:53855 helo=falcon.midgard.homeip.net) by ch-smtp01.sth.basefarm.net with esmtp (Exim 4.68) (envelope-from ) id 1Mhtwx-0004gH-5O for freebsd-questions@freebsd.org; Mon, 31 Aug 2009 01:36:45 +0200 Received: (qmail 12851 invoked from network); 31 Aug 2009 01:36:41 +0200 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with ESMTP; 31 Aug 2009 01:36:41 +0200 Received: (qmail 49129 invoked by uid 1001); 31 Aug 2009 01:36:41 +0200 Date: Mon, 31 Aug 2009 01:36:41 +0200 From: Erik Trulsson To: Stefan Miklosovic Message-ID: <20090830233641.GA48910@owl.midgard.homeip.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Originating-IP: 83.255.48.78 X-Scan-Result: No virus found in message 1Mhtwx-0004gH-5O. X-Scan-Signature: ch-smtp01.sth.basefarm.net 1Mhtwx-0004gH-5O 1d4a3daae51072c3489331754faaf8e9 Cc: freebsd-questions@freebsd.org Subject: Re: shell command line argument + parsing function X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 30 Aug 2009 23:36:54 -0000 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" -- Erik Trulsson ertr1013@student.uu.se