Skip to content

Instantly share code, notes, and snippets.

@srikanthmanda
Created July 18, 2021 17:25
Show Gist options
  • Save srikanthmanda/c9bd484e6682b43b05abc0d9ac208b41 to your computer and use it in GitHub Desktop.
Save srikanthmanda/c9bd484e6682b43b05abc0d9ac208b41 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Example:
# ./getopts-demo.sh -a42 -l Beeblebrox -c "Sector ZZ9 Plural Z Alpha" -f Zaphod
if [[ -z $1 ]]
then
echo 'Usage: ./getopts-demo.sh -f <first name> -l <last name> -a <age> -g <gender> -c <country>'
exit 1
fi
while getopts ":f:l:a:g:c:" option
do
echo "option: ${option}"
echo "OPTIND: ${OPTIND}"
echo "OPTARG: ${OPTARG}"
case ${option} in
f ) echo "First name: ${OPTARG}"
;;
l ) echo "Last name: ${OPTARG}"
;;
a ) echo "Age: ${OPTARG}"
;;
g ) echo "Gender: ${OPTARG}"
;;
c ) echo "Country: ${OPTARG}"
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment