If you want to print only the parameter that the user sent, you can simply use echo "$1". If you want to return to the default value, if the user did not send anything, you can use echo "${1:--l}( :-- Bash syntax for the default values). However, if you need a really powerful and flexible argument handling, you can look at getopt:
params=$(getopt --options f:v --longoptions foo:,verbose --name "my_script.sh" -- "$@")
if [ $? -ne 0 ]
then
echo "getopt failed"
exit 1
fi
eval set -- "$params"
while true
do
case $1 in
-f|--foo)
foobar="$2"
shift 2
;;
-v|--verbose)
verbose='--verbose'
shift
;;
--)
while [ -n "$3" ]
do
targets[${#targets[*]}]="$2"
shift
done
source_dir=$(readlink -fn -- "$2")
shift 2
break
;;
*)
echo "Unhandled parameter $1"
exit 1
;;
esac
done
if [ $# -ne 0 ]
then
error "Extraneous parameters." "$help_info" $EX_USAGE
fi
source
share