Learn the use of Command-Line Arguments in Linux Shell Scripting

0
5644

So far, we have used the read command to get input from the user. Command-Line Arguments is another way that allows the user to provide input(s) to the shell scripts on execution. This makes scripts imitate Linux shell commands (that most of them accept command-line arguments). In this article, we are going to learn how to make use of such arguments in our scripts.
 
Syntax:

Where arg1, arg2 through argn are the command-line arguments provided at script execution.
 
Special Variables
We had a short talk on this topic earlier in this series in Article (4) – Variables. In that article, we have seen two variables of this type: $? and $0.
Command-line arguments are accessed inside shell scripts’ code using another set of special variables. These variables are:
 

  • Positional Parameters: $1, $2, . . . , $n

that contain user-provided values for arg1, arg2, …, argn

  • $#                   the number of arguments passed to the script.
  • $*, $@            both variables have the same usage: they contain the list of arguments.

 
Example
Again and again, the factorial example, but with a fourth version that takes the input integer (for which the factorial will be calculated) as a command-line argument provided at execution.
Read this new version.
1

Let’s see it in actions.
2

Now, let’s explain it:
 

  • Line 6: the usual initialization step that sets the result to 1.
  • Line 7: starts the for loop, whose variable i will iterate on the sequence of integers from 1 to $1 (the first and only argument expected).
  • Line 9: the loop body that performs the accumulative multiplication:

result = result * i

  • Line 11: uses the echo command to print the result to the user.

 
In this modified version, notice the following:
 

  1. The read command was not used (and not needed), as the input is provided as a command-line argument.
  2. The $1 variable can be used like any other variable in the script; it was used as an argument to the seq command, and when printing the final result.

 
Example
In this example, we are going to write a script that expects the user to provide two numbers. The script then uses the two numbers as operands of the basic mathematical operations.
3

Let’s execute it:
4
Nothing here to be explained; just replaced variables x and y (that take their values from the read command in the previous version) by the positional parameters $1 and $2.
 
* * * * * *
 
String Conversion Example
In this example, we are going to write a script that expects string argument(s). The script should convert any alphabetic characters in the input into uppercase letters and print them.
5

Read the following script.
6

Let’s execute it and see what we will get:
7

Now, let’s explain how this works.
 

  • Line 6: uses the echo command to print the value of the variable $* (that stores the list of arguments passed on execution). The echo command output is then piped as an input to the command tr that will convert the input (content of the $* variable) to uppercase letters. The entire statement is enclosed within backticks ‘ ‘ to be assigned to the variable UPPERCASE.
  • Line 7: prints the converted input string to the user.

 
Learn the Basics of C Programming Language
 
Multiple Users Creation Example
This is my favorite star!! Actually, I love this example, as it is one of the first scripts I wrote when I started learning Linux. In this example, we are going to write a script that expects one or more arguments. The arguments expected are username accounts to be created on the Linux server. For every newly-created user account, a random password will be generated and assigned to it. Of course, the script should check whether the provided usernames already exist on the system. In this case, the script should report that.
 
8
Okay, we had useful hints that provide necessary keys to write this script. Having the keys in hand, let’s start creating our magic.
9

Let’s see it in action.
10

Now, hold your breath and open your eyes well to watch the magic!!!
11

Have you seen this?! Do you believe your eyes?! I have created 21 user accounts, and assigned them all passwords in one line!!! Even more, what if I replace the number 20 in the above execution by the number 100 or 200?! This will create 100 or 200 users, also in one line… formidable!!
 
Now, let me show you that these user accounts have been really created:
12

And they have been all assigned passwords:
13
Perfect!! Isn’t it?!
 
* * * * * *
 
In this article, we have talked about the Command-Line Arguments. We have seen how they could be useful in many situations.
The next article will discuss a new topic: Functions. So, don’t miss it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here