shell command line argument + parsing function
Bill Campbell
freebsd at celestial.com
Sun Aug 30 23:18:46 UTC 2009
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 at 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
More information about the freebsd-questions
mailing list