Handling parameters in Bash

Short Mode Only :


#!/bin/bash
while getopts :a:b:c opt #define options here : at the front supress error for unknown options
do
case $opt in
a) ARG1="${OPTARG}"
if [ "$ARG1" == "" ]
then
print_usage $0
else
echo "option a is " $ARG1
fi ;;
b) TYPE="$OPTARG";;
p) PRICES="$OPTARG";;
?) print_usage $0
exit 1;;
esac
done
echo "Balances: "$BAL
echo "Type: "$TYPE

#!/bin/bash
options=":hzx-:"
usage()
{
echo "You can display the Usage options here"
}
while getopts "$options" params; do
case "${params}" in
-)
case "${OPTARG}" in
option1)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2;
;;
option1=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
echo "Parsing option: '--${opt}', value: '${val}'" >&2
;;
help)
echo "usage: $0 -zx --option1= --option2="
;;
*)
if [ "$OPTERR" = 1 ] && [ "${options:0:1}" != ":" ]; then
echo "Unknown option --${OPTARG}" >&2
usage
fi
;;
esac;;
z)
val="${!OPTIND}";
echo "Parsing option: '-${params}' value:$val" >&2
;;
*)
if [ "$OPTERR" != 1 ] || [ "${options:0:1}" = ":" ]; then
echo "Non-option argument: '-${OPTARG}'" >&2
usage
fi
;;
h)
echo "usage: $0 [-v] [--loglevel[=]]" >&2
exit 2
;;
esac
done

You may also like...