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() {
> while [ $# -gt 0 ]; do
> case "$1" in
> -c)
> shift
> COMMENT="$1"
> ;;
> esac
> shift
> done
> }
>
> 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=$OPTARG
;;
\?)
echo 'usage: $0 [-c string]'
exit 1
;;
esac
done
echo "COMMENT: $COMMENT"
--
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>
