linux bash scripting
Linux

Linux bash scripting Part3 – Parameters and Options

So far you’ve seen how to write Linux bash scripts that interact with data, variables, files, and how to control the flow of the bash script. Today we will continue our series about Linux bash scripting.

I recommend you to review the previous posts if you want to know what we are talking about.

bash scripting part1

bash scripting part2

Today we will know how to retrieve input from the user and deal with that input, so our script becomes more interactive.

The common method of passing data to your shell script is to use command line parameters.

Command line parameters enable you to add data values to the command line when you execute the script.

$ ./myscript 10 20

This example passes two command line parameters (10 and 20) to the script. So how to read these parameters in our bash script?

Reading parameters

The bash shell assigns special variables for dealing with input parameters:

  • $0 is the script’s name.
  • $1 is the 1st parameter.
  • $2 is the 2nd parameter.

The last parameter is $9 which is for the 9th parameter.

Here’s a simple example of how to use command line parameters in a shell script

Check the results of the following command:

./myscript 5 10 15

linux bash scripting parameters

Here is another example of how we can use two parameters and calculate the sum of them.

linux-bash-scripting-parameters

The parameters are not restricted to numbers, they could be strings like this:

./myscript Adam

And the result is as expected.

linux-bash-scripting-string-parameters

What if our parameter contains a space and we want to pass it as one value? I guess you know the answer from the previous posts. The answer is to use quotations.

If your script requires over nine parameters, you need to use braces around the variable number, such as ${10}.

Check Parameters

If you don’t pass parameters and your code expecting it, you’ll get an error message from your script.

So always check your parameters using if statement to make sure that they exist.

linux-bash-scripting-testing-parameters

Counting parameters

You can count how many parameters were entered on the command line. The bash shell gives us a special variable for this purpose.

The special $# variable contains the number of the passed command line parameters.

./myscript 1 2 3 4 5

linux--bash-scripting-parameters-count

How awesome is Linux bash scripting? This variable also provides a geeky way of getting the last parameter on the command line without having to know how many parameters were passed. Look at this trick:

linux-bash-scripting-last-parameter

Grabbing all parameters

The $* and $@ variables give you all of the passed parameters. So you don’t have to grab them by $# variable and iterate over them, just one step.

The $* variable holds all the parameters supplied on the command line as a single word.

The $@ variable takes all the parameters as separate words within the same string, it enables you to iterate over them.

This code shows how to use them:

linux-bash-scripting-grab-all-parameters

The same result, but if you want to know the difference look at the following example:

Check the result to see the difference:

linux-bash-scripting-parameters-count

The $* variable handled all the parameters as a single parameter, while the $@ variable handled each parameter separately. So you can use any one of these variables according to your needs.

Shift command

The shift command has some little risk in Linux bash scripting, it literally shifts the command line parameters in their relative positions.

The shift command moves every parameter variable one position to the left by default, so the value for the variable $3 is shifted to $2, the value for variable $2 is shifted to $1, and the value for variable $1 is dropped (keep in mind that the value for variable $0, the script name, remains unchanged).

This is another way to iterate over parameters:

Here, the script performs a while loop, checking the length of the first parameter’s value. When The length of the first parameter is zero, the loop stops. After examining the first parameter, the shift command is utilized to shift all the parameters one position.

linux-bash-scripting-shift-command

Careful when using shift command because when a parameter is shifted, its value is removed and cannot be recovered.

Bash scripting options

Options are single letters with a dash before it, they modify the behavior of a command.

$ ./myscript -a -b -c -d

linux-bash-scripting-options

The code check for one of the correct options. If you typed one of them, the suitable commands will run.

Separate Options From Parameters

Sometimes you need to use options and parameters on the same script. You have to separate them. By doing this, you are telling the bash where are the parameters and where are the options.

The double dash (–) is used to end the options list. After the shell sees the double dash, the remaining inputs are treated as parameters and not as options.

$ ./myscript -a -b -c -- 5 10 15

linux-bash-scripting-options-with-parameters

As you can see from the result, all the parameters after the double dash treated as command line parameters.

Process Options With Values

When you dig deep onto Linux bash scripting, sometimes you need options with additional parameter values like this:

./myscript -a value1 -b -c value2

There should be a way to identify any additional parameter for the options and be able to process it.

And if we run it with these options:

$ ./myscript -a -b test1 -d

linux-bash-scripting-mix-options-with-parameters

From the results, you can see that we get the parameter for the -b option using the $2 variable.

 

Standardizing Options

When you start your Linux bash scripting, it’s completely up to you to choose which letter is suitable for your option.

However, there are some letters that have a standard meaning in the Linux world.

And here is the list of the common options:

-a            Shows all objects.

-c            Produces a count.

-d            Specifies a directory.

-e            Expands an object.

-f             To specify a file.

-h            To show help page.

-i             To ignore character case.

-l             To list a text.

-n            To say no for a question.

-o            To send output to a file or so.

-q            Runs in quiet mode.

-r             To process something recursively.

-s            Runs in silent mode.

-v            Produces verbose output.

-x            Excludes an object.

-y            To say yes without prompting the user.

If you work with Linux, many of these options may look familiar to you.

Using these letters as your options helps users to use your script without having to worry about manuals.

Getting User Input

Sometimes you need data from the user while the bash scripting is running.

The bash shell uses the read command for this purpose.

The read command reads input from standard input (the keyboard) or from a file descriptor. After that, the read command stores the data into a variable and here is an example:

Notice that the echo command that generates the prompt uses the -n option. This disables the newline character at the end of the string, this will allow you to enter data immediately after the string, instead of the next line.

linux-bash-scripting-read-input

You can specify multiple variables like this:

linux-bash-scripting-multi-input

If you don’t specify any variable for the read command the read command stores all incoming inputs in the special environment variable REPLY.

linux-bash-scripting-reply-variable

If you don’t need to wait for user input, you can use the -t option to specify a time. The -t option is used to specify the number of seconds to wait for input.

If you do not enter data for five seconds the script will execute the else clause and print a sorry message.

linux-bash-scripting-timeout

Reading password

In Linux bash scripting, sometimes you don’t want the user input to be displayed on the screen, like entering a password.

The -s option hides the data entered in the read command.

The data is actually printed on the screen, but the read command makes the text color as the background color of the shell.

linux-bash-scripting-hidden-input

Reading from a file

If you pass a file to the read command, the read command will read a single line from the file on each call.

Now, if you want to get all file data, you can get the content using the cat command, then send it to the read command using while loop like this:

linux-bash-scripting-read-from-file

We just pass the file content to while loop and iterate over every line and print the line number and the content, and each time you increase the count by one.

I hope you find this post interesting. Keep coming back.

Thank you.