Uses of Getopts and Getopt
Both getopts and getopt are used in Bash to read the named argument values. But there is a difference between these tools. Getopts is used to read the short option such as -h, -d, etc. But it can’t read the long options such as –version, –user, etc. Getopt is used to read the value of the long option. If you want to know more details about the getopts, you can check here.
Example 1: Read the Named Arguments Using Getopts
Create a Bash file with the following script that reads the two named arguments using getopts that support the short options. The -i and -r options are used to pass the named argument values during the execution of the script. Next, the argument value that is read by the -i option is checked with a particular value to print the output based on the matching value.
while getopts "i:r:" var
do
case "$var" in
i) ID=${OPTARG};;
r) GPA=${OPTARG};;
esac
done
#Print message based on the matching ID value passed in the argument
if [[ "$ID" == "56" ]]; then
echo "Mir Sabbir obtained $GPA"
elif [[ "$ID" == "34" ]]; then
echo "Nirob Ahsan obtained $GPA"
else
echo "ID is invalid."
fi
The script is executed twice in the following output. The script is executed without any argument in the first execution. So, the error message is displayed. The script is executed with two valid named arguments in the second execution. The formatted values of the named argument values are printed in the output:
Example 2: Read the Named Arguments Using Getopt
Create a Bash file with the following script that reads the two named arguments using getopt that supports the long options. The –email and –pass options are used to pass the named argument values during the execution of the script. The argument values that are read by the named arguments are stored in two variables named $Email and $Password. Next, the values of these variables are compared with the particular values and print the output based on the output of the comparison.
#Set the options of the getopt command
format=$(getopt -n "$0" -l "email:,pass:" -- -- "$@")
if [ $# -lt 3 ]; then
echo "Wrong number of arguments are passed."
exit
fi
eval set -- "$format"
#Read the argument values
while [ $# -gt 0 ]
do
case "$1" in
--email) Email="$2"; shift;;
--pass) Password="$2"; shift;;
--) shift;;
esac
shift;
done
#Compare the argument values with the particular value
if [[ "$Email" == "[email protected]" && "$Password" == "secretpass" ]]; then
echo "Valid user"
else
echo "Invalid user"
fi
The script is executed thrice in the following output. The script is executed without any argument in the first execution. So, the error message “Wrong number of arguments are passed” is displayed. The script is executed with two argument values in the second execution but the value of the second argument didn’t match the password value. So, the message “Invalid user” is displayed. The script is executed with two valid named arguments in the third execution and both argument values matched with the values that are provided in the “if” condition. So, the message “Valid user” is displayed.
Example 3: Read the Named Argument without Getopts and Getopt
Create a Bash file with the following script that reads the three named arguments using the “while” loop. According to the script, both short and long options can be used to pass the named arguments in the script. The -u or –user, -p or –pass, and -h or –host options are used to pass the three named argument values during the execution of the script. Next, the argument values are read and stored into three variables using the “while” loop and the “shift” command.
#Read the argument values
while [[ "$#" -gt 0 ]]
do
case $1 in
-u|--user) User="$2"; shift;;
-p|--pass) Password="$2"; shift;;
-h|--host) Host="$2"; shift;;
esac
shift
done
#Print the argument values
printf "username: $User \npassword: $Password \nhostname: $Host\n"
The script is executed with three valid named arguments in the following output. So, the values of the three arguments are printed:
Conclusion
The different ways of using the named arguments in the Bash script are shown in this tutorial to help the Bash users know how to use the named arguments.