Friday, September 7, 2018

Command Line Arguments

Check the following command line:
student@ubuntu:~$ my_program arg1 arg2 arg3
If my_command is a bash Shell script, then we can access every command line positional parameters inside the script as follows:

$0 would contain "my_program" # Command
$1 would contain "arg1" # First parameter
$2 would contain "arg2" # Second parameter
$3 would contain "arg3" # Third parameter

The following is the summary of positional parameters:

$0                       Shell script name or command
$1–$9                 Positional parameters 1–9
${10}                 Positional parameter 10
$#                       Total number of parameters
$*                       Evaluates to all the positional parameters
$@                     Same as $*, except when double quoted
"$*"                    Displays all parameters as "$1 $2 $3", and so on
"$@"                   Displays all parameters as "$1" "$2" "$3", and so on


Let's create a script param.sh as follows:

#!/bin/bash
echo "Total number of parameters are = $#"
echo "Script name = $0"
echo "First Parameter is $1"
echo "Second Parameter is $2"
echo "Third Parameter is $3"
echo "Fourth Parameter is $4"
echo "Fifth Parameter is $5"
echo "All parameters are = $*"

Then as usual, give execute permission to script and then execute it:

./parameter.sh London Washington Delhi Dhaka Paris

Output:
Total number of parameters are = 5
Command is = ./parameter.sh
First Parameter is London
Second Parameter is Washington
Third Parameter is Delhi
Fourth Parameter is Dhaka
Fifth Parameter is Paris
All parameters are = London Washington Delhi Dhaka Paris

Many times we may not pass arguments on the command line, but we may need to
set parameters internally inside the script.
We can declare parameters by the set command as follows:

$ set USA Canada UK France
$ echo $1
USA
$ echo $2
Canada
$ echo $3
UK
$ echo $4
France

We can use this inside the set_01.sh script as follows:

#!/bin/bash
set USA Canada UK France
echo $1
echo $2
echo $3
echo $4

Run the script as:
$ ./set.sh
Output:
USA
Canada
UK
France

Let's write script set_03.sh as follows:

#!/bin/bash
echo "Executing script $0"
echo $1 $2 $3
set eins zwei drei
echo "One two three in German are:"
echo "$1"
echo "$2"
echo "$3"
textline="name phone address birthdate salary"
set $textline
echo "$*"
echo 'At this time $1 = ' $1 'and $4 = ' $4

In this script, the following output shows:
1. Initially when the set is not called, then $1, $2, $3 do not contain any
information.
2. Then, we set $1 to $3 as GERMAN numerals in words.
3. Then, set $1 to $5 as name, phone, address, birthdate, and salary, respectively.

Using shift, we can change the parameter to which $1 and $2 are pointing to the next variable.
Create a script shift_01.sh as follows:

#!/bin/bash
echo "All Arguments Passed are as follow : "
echo $*
echo "Shift By one Position :"
shift
echo "Value of Positional Parameter $ 1 after shift :"
echo $1
echo "Shift by Two Positions :"
shift 2
echo "Value of Positional Parameter $ 1 After two Shifts :"
echo $1

Execute the command as follows:
$ chmod +x shift_01.sh
$ ./shift_01.sh One Two Three Four
Output:
student@ubuntu$ ./shift_01.sh One Two Three Four

All arguments passed are as follows:

One Two Three Four

Shift by one position.

Here, the value of the positional parameter $1 after shift is:

Two

Shift by two positions.

The value of the positional parameter $1 after two shifts:
Four

We observed that initially $1 was One. After shift, $1 will be pointing to Two.
Once shift is done, the value in position 1 is always destroyed and is inaccessible.

Create a shift_02.sh script as follows:



#!/bin/bash
echo '$#: ' $#
echo '$@: ' $@
echo '$*: ' $*
echo
echo '$1 $2 $9 $10 are: ' $1 $2 $9 $10
echo
shift
echo '$#: ' $#
echo '$@: ' $@
echo '$*: ' $*
echo
echo '$1 $2 $9 are: ' $1 $2 $9
shift 2
echo '$#: ' $#
echo '$@: ' $@
echo '$*: ' $*
echo
echo '$1 $2 $9 are: ' $1 $2 $9
echo '${10}: ' ${10}


In this script execution, the following output shows:
1. Initially, $1 to $13 were numerical values 1 to 13, respectively.
2. When we called the command shift, then $1 shifted to number 2 and accordingly all $numbers are shifted.
3. When we called the command shift 2, then $1 shifted to number 4 and accordingly all $numbers are shifted.

In certain situations, we may need to reset original positional parameters.
Let's try the following:
set Alan John Dennis
This will reset the positional parameters.
Now $1 is Alan, $2 is John, and $3 is Dennis.
Inside the scripts, we can save positional parameters in a variable as follows:
oldargs=$*
Then, we can set new positional parameters.
Later on, we can bring back our original positional parameters as follows:
set $oldargs

Command line parameters passed along with commands are also called as positional parameters. Many times, we need to pass options such as –f and -v along with positional parameter.
Let's learn the example for passing the –x or –y options along with commands.
Write the Shell script getopt.sh as follows:



#!/bin/bash
USAGE="usage: $0 -x -y"
while getopts :xy: opt_char
do
case $opt_char in
x)
echo "Option x was called."
;;
y)
echo "Option y was called. Argument called is $OPTARG"
;;
\?)
echo "$OPTARG is not a valid option."
echo "$USAGE"
;;
esac
done


Execute this program:
$ ./getopt.sh
You will be learning switch and case statements in the next chapters. In this script, if option –x is passed, a case statement for x will be executed. If the –y option is passed,then a case statement for –y will be executed. Since no option is passed, there will not be any output on the screen.
$ ./getopt.sh –x
Output:
Option x was called."
$ ./getopt.sh –y my_file
Output:
Option y was called. Argument called is my_file.
$ ./getopt.sh –x –y my_file
Output:
Option x was called.
Option y was called. Argument called is my_file.
$ ./getopt.sh –y my_file -x
Output:
Option y was called. Argument called is my_file.
Option x was called.

Many times we may pass certain parameters from the command line; sometimes, we may not pass any parameters at all. We may need to have certain default values to be initialized to certain variables

Create a default_argument_1.sh script as follows:

#!/bin/bash
MY_PARAM=${1:-default}
echo $MY_PARAM

Execute the script and check:
$ chmod +x default_argument_1.sh One
$ ./default_argument_1.sh One
One
$ ./default_argument_1.sh
default

Create another default_argument_2.sh script:

#!/bin/bash
variable1=$1
variable2=${2:-$variable1}
echo $variable1
echo $variable2

We executed the script two times:
• When we passed two arguments, then variable1 was $1 and variable2
was $2.
• In second case, when we passed only one argument, then $1 was taken
as a default argument for $2. Therefore, variable1 was used as default
variable2. If we do not give a second parameter, then the first parameter
is taken as a default second parameter.


No comments:

Post a Comment