Have you ever met thus kind of requirement:
Use BASH to write a script with options to implements some functions??
For example, here is the requirement.. "We need a script in BASH to get its options and save them into environments for further using. options include -h, -n name, -v version, -r release, -o output. And the output should be like the followings:"
[xiaoqiang@Linux01:~/bin] $./get_args.sh
[xiaoqiang@Linux01:~/bin] $./get_args.sh -h
./get_args.sh -n name -v version -r release -o output
[xiaoqiang@Linux01:~/bin] $./get_args.sh -n Xiaoqiang -v 1.0 -r 01 -o ~/
GETARGS_NAME=Xiaoqiang
GETARGS_OUTPUT=/home/xiwang/
GETARGS_RELEASE=01
GETARGS_VERSION=1.0
Now, the script you may needed:
#!/bin/bash
#
# Example of getting arguments from command line.
#
# get_args.sh, 2012-06-08 T1256
#
# for example:
# ./get_args.sh -h
# ./get_args.sh -n name -v version -r release -o output
#
while [ $# -ne 0 ]; do
case "$1" in
-h|--help)
echo "./get_args.sh -n name -v version -r release -o output"
exit 0
;;
-n|--name)
GETARGS_NAME="$2"; export GETARGS_NAME
shift; shift;
;;
-v|--version)
GETARGS_VERSION="$2"; export GETARGS_VERSION
shift; shift;
;;
-r|--release)
GETARGS_RELEASE="$2"; export GETARGS_RELEASE
shift; shift;
;;
-o|--output)
GETARGS_OUTPUT="$2"; export GETARGS_OUTPUT
shift; shift;
;;
*)
echo "Error, no such option or argument not supported."
exit 1
;;
esac
done
set | grep GETARGS_