getopts is used in scripting to parse command line arguments. The arguments passed on the command line are parsed in the script using a string which is a string made by separating the argument names by colons.
For example if the arguments passed were named a,b,c,d, the string would be made up as follows a:b:c:d where the colon after the name specifies that the variable is expected to be associated with a value on the command line.
Internally getopts uses OPTIND to index the arguments and OPTARG to read the value of arguments.
Here is an example :
#!/bin/bash
while getopts a:b:c:d: opts # using getopts to parse the command line options
do
echo "index $OPTIND" # printing the index of the argument being parsed
case "$opts" in
a) one="$OPTARG" # Using OPTARG to get the value of the argument
echo $OPTARG ;;
b) two="$OPTARG"
echo $OPTARG ;;
c) three="$OPTARG"
echo $OPTARG ;;
d) four="$OPTARG"
echo $OPTARG ;;
esac
done
echo " one = $one two = $two three = $three four = $four"
Save the above script as gopts_example.sh and execute it as follows
sh getopts a 1 b 2 c 3 d 4
output :
index 3
1
index 5
2
index 7
3
index 9
4
one = 1 two = 2 three = 3 four = 4
From the output we see that the OPTIND stores the index of argument it is reading and OPTARG stores the value.
The index can be reintialized to "1" to restart the parsing of the arguments, as shown in the script below.
#!/bin/bash
while getopts a:b:c:d: opts # using getopts to parse the command line options
do
echo "index $OPTIND" # printing the index of the argument being parsed
case "$opts" in
a) one="$OPTARG" # Using OPTARG to get the value of the argument
echo $OPTARG ;;
b) two="$OPTARG"
echo $OPTARG ;;
c) three="$OPTARG"
echo $OPTARG ;;
d) four="$OPTARG"
echo $OPTARG ;;
esac
done
echo " one = $one two = $two three = $three four = $four"
OPTIND=1 # Reinitialize the OPTIND to 1
while getopts a:b:c:d: opts # Repeat the same loop as above.
do
echo "index $OPTIND"
case "$opts" in
a) one="$OPTARG"
echo $OPTARG ;;
b) two="$OPTARG"
echo $OPTARG ;;
c) three="$OPTARG"
echo $OPTARG ;;
d) four="$OPTARG"
echo $OPTARG ;;
esac
done
As opposed to the standard way of parsing the command line arguments using the $ symbol, it is simpler to parse it using the getopts as we need not keep track of the index numbers of the arguments being passed and the number of arguments being passed.
Even if the order of the arguments passed is not the same as mentioned in the string, i.e. a:b:c:d in our case, getopts would still be able to parse it successfully.
If any of the expected arguments is not passed on the command line then it is assumed to be blank and no error is thrown.
For example if the arguments passed were named a,b,c,d, the string would be made up as follows a:b:c:d where the colon after the name specifies that the variable is expected to be associated with a value on the command line.
Internally getopts uses OPTIND to index the arguments and OPTARG to read the value of arguments.
Here is an example :
#!/bin/bash
while getopts a:b:c:d: opts # using getopts to parse the command line options
do
echo "index $OPTIND" # printing the index of the argument being parsed
case "$opts" in
a) one="$OPTARG" # Using OPTARG to get the value of the argument
echo $OPTARG ;;
b) two="$OPTARG"
echo $OPTARG ;;
c) three="$OPTARG"
echo $OPTARG ;;
d) four="$OPTARG"
echo $OPTARG ;;
esac
done
echo " one = $one two = $two three = $three four = $four"
Save the above script as gopts_example.sh and execute it as follows
sh getopts a 1 b 2 c 3 d 4
output :
index 3
1
index 5
2
index 7
3
index 9
4
one = 1 two = 2 three = 3 four = 4
From the output we see that the OPTIND stores the index of argument it is reading and OPTARG stores the value.
The index can be reintialized to "1" to restart the parsing of the arguments, as shown in the script below.
#!/bin/bash
while getopts a:b:c:d: opts # using getopts to parse the command line options
do
echo "index $OPTIND" # printing the index of the argument being parsed
case "$opts" in
a) one="$OPTARG" # Using OPTARG to get the value of the argument
echo $OPTARG ;;
b) two="$OPTARG"
echo $OPTARG ;;
c) three="$OPTARG"
echo $OPTARG ;;
d) four="$OPTARG"
echo $OPTARG ;;
esac
done
echo " one = $one two = $two three = $three four = $four"
OPTIND=1 # Reinitialize the OPTIND to 1
while getopts a:b:c:d: opts # Repeat the same loop as above.
do
echo "index $OPTIND"
case "$opts" in
a) one="$OPTARG"
echo $OPTARG ;;
b) two="$OPTARG"
echo $OPTARG ;;
c) three="$OPTARG"
echo $OPTARG ;;
d) four="$OPTARG"
echo $OPTARG ;;
esac
done
As opposed to the standard way of parsing the command line arguments using the $ symbol, it is simpler to parse it using the getopts as we need not keep track of the index numbers of the arguments being passed and the number of arguments being passed.
Even if the order of the arguments passed is not the same as mentioned in the string, i.e. a:b:c:d in our case, getopts would still be able to parse it successfully.
If any of the expected arguments is not passed on the command line then it is assumed to be blank and no error is thrown.
No comments:
Post a Comment