Date: Tue, 1 Sep 2009 11:19:35 +0300 From: Artis Caune <artis.caune@gmail.com> To: Stefan Miklosovic <miklosovic.freebsd@gmail.com> Cc: freebsd-questions@freebsd.org Subject: Re: shell command line argument + parsing function Message-ID: <9e20d71e0909010119g387c3869pb61a59c8b3c73c5b@mail.gmail.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
2009/8/31 Stefan Miklosovic <miklosovic.freebsd@gmail.com>: > 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() { > =C2=A0 =C2=A0while [ $# -gt 0 ]; do > =C2=A0 =C2=A0 =C2=A0 =C2=A0case "$1" in > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-c) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0shift > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0COMMENT=3D"$1" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;; > =C2=A0 =C2=A0 =C2=A0 =C2=A0esac > =C2=A0 =C2=A0 =C2=A0 =C2=A0shift > =C2=A0 =C2=A0done > } > > parse_cmdline $* > > echo $COMMENT > > exit 0 How about getopts builtin, so you can use: ./script -c "hello world" or ./script -c"hello world" or while getopts c: f; do case $f in c) COMMENT=3D$OPTARG ;; \?) echo 'usage: $0 [-c string]' exit 1 ;; esac done echo "COMMENT: $COMMENT" --=20 Artis Caune Everything should be made as simple as possible, but not simpler.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9e20d71e0909010119g387c3869pb61a59c8b3c73c5b>