Wednesday, 6 January 2016

Positional Parameter In Unix/Linux Scripts

This article will describe how parameters are passed to Unix/Linux shell scripts. Like any other language where we pass parameter to functions, procedures or packages similarly we can pass parameter to Unix scripts. Let see how to define parameter and associated syntax.

Script Name: Pos_Parameter_Example.sh

$ cat Pos_Parameter_Example.sh
echo "1st Parameter is :" $1
echo "2nd Parameter is :" $2
echo "3rd Parameter is :" $3
echo "All Parameter are:" $@

echo "Script Name is:" $0
echo "Total Number of Parameter is :" $#

$ sh Pos_Parameter_Example.sh unix linux windows


Output:
1st Parameter is : unix
2nd Parameter is : linux
3rd Parameter is : windows
All Parameter are: unix linux windows

Script Name is: Pos_Parameter_Example.sh
Total Number of Parameter is : 3

Below is the syntax to provide parameter to the shell script.

SYNTAX: shell_Script.sh <parm1> <parm2> <parm3> ... <parmN>

Explanation:

1. $1 is used to hold value of 1st parameter, $2 for 2nd and so on.
2. $@ is used to hold the complete list of parameters.
3. $# is used to hold the count of parameter passed to the Unix/Linux script.

4. $0 is used to hold the script name itself.

Let say you have a script, see below, which takes 11 parameters.

$ cat Pos_Parameter_Example.sh
echo "1st Parameter is :" $1
echo "2nd Parameter is :" $2
echo "9th Parameter is :" $9
echo "10th Parameter is :" $10
echo "11th Parameter is :" $11

$ sh Pos_Parameter_Example.sh a b c d e f g h i j k


Output:
1st Parameter is : a
2nd Parameter is : b
9th Parameter is : i
10th Parameter is : a0
11th Parameter is : a1


Explanation:
1. value of the 10th positional parameter is a0 that is not correct as we are expecting it to j.value of the 11th positional parameter is a1 that is not correct as we are expecting it to k. Here script is not able to differentiate between $1 and $10, it has interpreted the $10 as $1"0" hence assigned the value of $1 and concatenated it with 0 to result a0

How can we overcome with this situation? The simplest way is to enclose it in curly braces{}, how? 

Let see the updated script below:

$ cat Pos_Parameter_Example.sh
echo "1st Parameter is :" ${1}
echo "2nd Parameter is :" ${2}
echo "9th Parameter is :" ${9}
echo "10th Parameter is :" ${10}
echo "11th Parameter is :" ${11}

$ sh Pos_Parameter_Example.sh a b c d e f g h i j k
Output:
1st Parameter is : a
2nd Parameter is : b
9th Parameter is : i
10th Parameter is : j
11th Parameter is : k

Note: We should always use curly braces not only for positional parameters but also while fetching the value of variable. This is a good coding practice to use curly braces.

Example:
V_First_Number=10
$ echo $V_First_Number
Output: 10
$ echo ${V_First_Number}
Output: 10

Though both the syntax has provided same output but we should always use the 2nd way of writing (using curly braces). 

If you pass a sentence as a parameter

$ cat Pos_Parameter_Example.sh
echo "1st Parameter is :" ${1}
echo "2nd Parameter is :" ${2}

$ sh Pos_Parameter_Example.sh Unix System Linux System
Output:
1st Parameter is : Unix
2nd Parameter is : System

Opps, This is not what we are expecting. We have received this output because by default parameters are space separated.To convert this sentence as a single parameter we have to place it in between double quotes.

$ sh Pos_Parameter_Example.sh "Unix System" "Linux System"

Output:
1st Parameter is : Unix System
2nd Parameter is : Linux System


Conclusion: We will face number of situation where we need to provide some parameters to the script and based on that parameter script will invoke concerned operation. By using parameters we can make generic scripts that can be used across multiple application and perform according the input provided.

Keep Reading, Keep Learning, Keep Sharing...!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...