Linux bash scripting
Linux

Linux Bash Scripting Part3 – Parameters and Options

So far you’ve seen how to write Linux bash scripts that do the job without user inputs. 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.

To pass data to your shell script, you should use command line parameters.

$ ./myscript 3 5

Here we send 2 parameters (3 and 5) to the script. So how to read these parameters in our bash script?

Read Parameters

The shell gives you some read to use variables to process input parameters:

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

Until the 9th parameter which is $9.

Let’s see those variables in action:

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.

$ ./myscript 5 10

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 should use braces like this ${10}.

Check Parameters

If you don’t pass parameters and your code expecting it, your script will exit with an error.

That’s why we should an if statement to make sure that they exist.

linux-bash-scripting-testing-parameters

Counting Parameters

To get how many parameters passed, you can use this variable ($#).

./myscript 1 2 3 4 5

linux--bash-scripting-parameters-count

How awesome is Linux bash scripting? This variable also provides a geeky way to get the last parameter. Look at this trick:

linux-bash-scripting-last-parameter

Grabbing All Parameters

The $* variable holds all the parameters as one value.

The $@ variable holds all the parameters as separate values, so you can 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 result is pretty clear. You can use any one of them according to your needs.

Shift command

The shift command moves every parameter variable to the left:

variable $3 ==> variable $2.

variable $2 ==> variable $1.

variable $1 ==> dropped.

variable $0 ==> (the script name) as it is.

You can use the shift command to iterate over parameters like this:

Here, we have a while loop checking $1 length. If $1 becomes zero, the loop stops. And the shift command is shifting all passed parameters to the left.

linux-bash-scripting-shift-command

Careful when using shift command, the shifted parameter is gone and cannot be recovered.

Bash Scripting Options

Options are single letters with a dash before it.

$ ./myscript -op1 -op2 -op3 -op4

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 parameters and not options.

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.

Standard Options

When you start your Linux bash scripting, you are free to choose which letter is suitable for your option.

However, there are some letters that are commonly used in Linux programs.

And here is the list of the common options:

-a            List all items.

-c            Get count of items.

-d            Output directory.

-e            Expands items.

-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            Keep silent don’t ask the user.

-r             To process something recursively.

-s            Stealth mode.

-v           Verbose mode.

-x            Specify executable.

-y            To say yes without prompting the user.

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

it’s good to follow standards.

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 and stores it in a variable:

The -n option is used to disable the newline, so you can type your text in the same line.

linux-bash-scripting-read-input

You can specify multiple inputs like this:

linux-bash-scripting-multi-input

If you don’t specify variables for the read command, all incoming inputs are saved in REPLY variable.

linux-bash-scripting-reply-variable

You can use the -t option to specify a timeout for input in seconds.

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

Read Files

The read command can read files one line 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 the 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.