Passing Arguments and using “If, elif, else”

To get started with the basics of bash shell scripting click here.

1 . Passing Argument to Bash Script

Create a file, open using a text editor.

$ touch arg.sh
$ Chmod +x arg.sh
$ nano arg.sh
#!/bin/bash

echo $1 $2 $3 '> echo $1 $2 $3'

Here we have three variables $1, $2 and $3. We pass three numbers 21, 22 and 23 as arguments so these inputs goes into the variables $1, $2 and $3.



We can also include name of the shell script by adding variable $0.

#!/bin/bash

echo  $0 $1 $2 $3 '> echo $0 $1 $2 $3'


Passing arguments as an array :

Remove everything and write a new script to pass arguments as an array. We will create a new variable args having value $@.

$@ refers to all of a shell script’s command-line arguments. So basically it’s a default variable in which arguments are stored as array.

#!/bin/bash

args=("$@")
echo ${args[0]} ${args[1]} ${args[2]} ${args[3]}


But here we will not get the script name because only the inputs will be included in array, here index [0] will be the first input that is 21.

To print all the arguments at once:

We will use echo $@ it will print all the arguments given.

#!/bin/bash

echo $@


Counting number of arguments :

To count number of arguments passed we use $#.

#!/bin/bash

echo $@
echo $#

Here we get three as output.



2 . If Statement ( if, elif or else)

If statement effectively says, if a particular test is true, then perform a given set of actions. If it is false then don’t perform those actions. If follows the format below:

if [ <some test> ]
then
<commands>
fi

Anything between then and fi (if backwards) will be executed only if the test (between the square brackets) is true.

Let’s take an example, create a file and open with text editor :

$ nano if.sh
$ chmod +x if.sh
#!/bin/bash

count=10

if [ $count -eq 9 ]
then
    echo "condition is true"
fi

Here we have count = 10, and along with if statement we have written a condition that if count is equal to 10 then condition is true. Save the script and then execute.



Nothing will be printed because it does not satisfies the condition. So now we will make changes and then again execute the script.

#!/bin/bash

count=10

if [ $count -eq 10 ]
then
    echo "condition is true"
fi

Now count is equal to 10, hence the statement is true and we get the following output:



You can also use if statement for comparing words, lets make changes in our script :

#!/bin/bash

word=abc

if [ $word = 'abc' ]
then
    echo "condition is true"
fi

Here we are using word = abc, the if statement says if word = abc then it will be true. Note that we need to put abc inside single commas ( ‘ ) or double commas ( ” ).



If else

Sometimes we want to perform a certain set of actions if a statement is true, and another set of actions if it is false, with the else mechanism.

if [ <some test> ]
then
<commands>
else
<other commands>
fi

Let’s use else statement in our script and the execute:

#!/bin/bash

word=abc

if [ $word = 'def' ]
then
    echo "condition is true"
else
    echo "condition is false"
fi

Here we have condition if word = abc then it is true or else condition is false. When we execute the script we get the output :



If elif else

Sometimes we have a multiple conditions that may lead to different paths. You can have as many elif branches as you like. The final else is also optional.

if [ <some test> ]
then
<commands>
elif [ <some test> ] 
then
<different commands>
else
<other commands>
fi

We will put elif statement in our script:

#!/bin/bash

word=abc

if [ $word = 'def' ]
then
    echo "condition a is true"
elif [ $word = 'abc' ]
then
    echo "condition b is true"    
else
    echo "condition is false"
fi

After if statement we have elif statement which provides another set of conditions and then the else statement. After executing the script we get output:



To learn more about bash scripting move to the next blog.