From: Jack on

I use the following while loop to parse commandline options.

while getopts 'a:b:h' OPTION
do
case $OPTION in
a)ARCHIV="$OPTARG"
;;
b)BACKUP="$OPTARG"
;;
h)printf "Usage: %s [-a value] \n [-b value] \n" $(basename
$0) >&2
exit 2
;;
?)printf "Usage: %s [-a value] \n [-b value] args\n" $
(basename $0) >&2
exit 2
;;
esac
done

How to write a function for this? If I put the while loop in to a
function as below, how to pass OPTION into the function?

func()
{
while getopts 'a:b:h' OPTION
do
case $OPTION in
a)ARCHIV="$OPTARG"
;;
b)BACKUP="$OPTARG"
;;
h)printf "Usage: %s [-a value] \n [-b value] \n" $(basename
$0) >&2
exit 2
;;
?)printf "Usage: %s [-a value] \n [-b value] args\n" $
(basename $0) >&2
exit 2
;;
esac
done


}


Thanks.
From: Bill Marcum on
On 2010-05-13, Jack <junw2000(a)gmail.com> wrote:
>
> I use the following while loop to parse commandline options.
>
>
> How to write a function for this? If I put the while loop in to a
> function as below, how to pass OPTION into the function?
>
Invoke the function as func "$@"


> func()
> {
> while getopts 'a:b:h' OPTION
> do
> case $OPTION in
> a)ARCHIV="$OPTARG"
> ;;
> b)BACKUP="$OPTARG"
> ;;
> h)printf "Usage: %s [-a value] \n [-b value] \n" $(basename
> $0) >&2
> exit 2
> ;;
> ?)printf "Usage: %s [-a value] \n [-b value] args\n" $
> (basename $0) >&2
> exit 2
> ;;
> esac
> done
>
>
> }
>
>
> Thanks.


--
[It is] best to confuse only one issue at a time.
-- K&R
 | 
Pages: 1
Prev: Using wget
Next: Question about using ls