Linux Commands

Linux “getopts” Example

“getopts” is a very useful command in Linux that is mostly incorporated within bash scripts to take flags and arguments as inputs from the user. To further elaborate on the usage of this command in Linux Mint 20, we will be sharing with you two different examples of its usage in this article.

“getopts” Examples in Linux Mint 20:

For an easier and a better understanding of the “getopts” command in Linux Mint 20, we have designed for you two example scenarios. Before heading on to these scenarios, you can check out the help manual of the “getopts” command in Linux Mint 20 terminal so that you may get the basic idea about the syntax of this command. You can see the help manual of this command by executing the “help” command:

$ getopts --help

The help manual of the “getopts” command, will contain a brief description of the usage of this command along with its syntax as shown in the image below:

Now when we have gone through the help manual of the “getopts” command, we can proceed with our example scenarios as follows:

Case # 1: Passing Flags to a Bash Script for its Execution in Linux Mint 20:

In this example, we will be creating a bash script that will take a flag as input from the user along with the execution command of that bash script. Depending upon the value of the provided flag, the respective output will be displayed. The detailed description of the steps involved in this scenario is as follows:

Step # 1: Writing a Bash Script:

First, we will write a bash script in our Home directory. We have named this file as getopts.sh. The contents of this bash script are shown in the image below. In the bash script shown below, we have a “while” loop that runs on the “getopts” command. Here, this “getopts” command is used to take a flag as input from the user. The value of this flag can either be “r”, “y”, or “g” representing the “red”, “yellow”, and “green” colors respectively. The “option” variable will be used to store the value of the flag provided by the user. Then we have used a “case” statement on the value of the provided flag which will display a customized message depending upon the value of the “option” flag. Finally, we have closed the “case” block with the “esac” statement and the “do” block with the “done” statement.

Step # 2: Testing the Bash Script:

After creating and saving our bash script, now it is time to run it. We will be testing all the cases one by one. First, we will run the bash script with the “-r” flag to see if it produces the correct output or not in the following manner:

$ bash getopts.sh –r

The customized output of our bash script with the “-r” flag is shown in the image below:

Now, we will run the bash script with the “-y” flag to see if it produces the correct output or not in the following manner:

$ bash getopts.sh –y

The customized output of our bash script with the “-y” flag is shown in the image below:

Now, we will run the bash script with the “-g” flag to see if it produces the correct output or not in the following manner:

$ bash getopts.sh –g

The customized output of our bash script with the “-g” flag is shown in the image below:

Finally, we will run our bash script with any random flag, i.e. a flag that is not valid or a flag that is not mentioned in our bash script says “-a” in the following manner:

$ bash getopts.sh –a

This time, the default case of our bash script has been executed which has displayed an error message since we have used an invalid flag.

Case # 2: Passing Flags along with Arguments to a Bash Script for its Execution in Linux Mint 20:

In this example, we will be creating a bash script that will take a flag and an argument as input from the user along with the execution command of that bash script. Depending upon the value of the provided flag and argument, the respective output will be produced. The detailed description of the steps involved in this scenario is as follows:

Step # 1: Writing a Bash Script:

The bash script used for this case is shown in the image below and is very much similar to the one we created for our case # 1. The only difference is that this time, we are not allowing multiple flags. The user is only allowed to enter the “-r” flag. However, the “:” colon after the “r” flag in the bash script indicates that the user is also supposed to provide an argument along with the flag. This argument will be stored in the default “OPTARG” variable. For displaying the value of the provided argument with the “echo” command, we have used a “$” symbol before the “OPTARG” variable.

Step # 2: Testing the Bash Script:

After saving this bash script, now is the time to test it. Again, we will be testing all the cases one by one. First, we will run the bash script with the “-r” flag and a sample argument “black” to see if it produces the correct output or not in the following manner:

$ bash getopts.sh –r black

The customized output of our bash script with the “-r” flag and “black” argument is shown in the image below:

Now, we will test this script with an invalid flag and a valid argument in the following manner:

$ bash getopts.sh –a black

This time, the default case of our bash script has been executed which has displayed an error message since we have used an invalid flag.

Now, we will run this bash script while omitting the flag and just passing a valid argument in the following manner:

$ bash getopts.sh black

This time, no output will be produced on the terminal or in other words, our bash script will not execute since we have skipped a mandatory flag.

Finally, we will run this bash script while omitting the argument and just passing the valid flag in the following manner:

$ bash getopts.sh –r

Again this time, the default case of our bash script has been executed which has displayed an error message since we have skipped the mandatory argument.

Conclusion:

These two examples are a very nice depiction of the usage of the “getopts” command in a bash script in Linux Mint 20. You can even create more complex scenarios in bash and pair them up with the “getopts” command to take more than one argument and flags from the user as inputs.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.