Saturday, March 3, 2018

Learn Shell Scripting -- Part 2

We could use “if” statement when it is required to perform a conditional based operation. So, if the condition is true then process a set of commands, otherwise, process some other commands/action. The syntax of this is:

< statement > <--------- these statements gets executed if 'variable' matches to none of the patterns

esac

NOTE: Each pattern statements should end with ";;" and end of the case statement should terminate with "esac".

Practice Example 1:

--------------------------

I’d use the same example as stated above (used in nested if-loop) in this script to demonstrate usage of “case” statement. The script should be as shown below:

Let’s execute the script and test if that works as expected:

As you could see the variable "NUM" is evaluated and depending on number entered the respective case statements gets processed.

- If NUM is 0 then it would print "You've entered : 0 (standard input)”.

- if NUM is 1 then it would print "You've entered : 1 (standard output)”.

- If NUM is 2 then it would print "You've entered : 2 (standard error)”.

- If NUM is other than 0 or 1 or 2 then it would print "Unable to understand the input!".

Practice Example 2:

--------------------------

In this example, I’d create a shell script which could print out Linux distribution vendor name depending on the user input. So, if user enters “rhel” then it would print the vendor name as “Red Hat” as shown below (this is just an example):

Please remember to set “execute” bit in each example script and then run the script. You might have noticed that I've used two words in each case loop here, so, it could match out either of the word (lower-case|upper-case). This is how it prints data when executed:

Usage of “for” loop (iteration) statement

Whenever there is a necessity to repeat a set of statements/commands then we could use “for” loop. This is considered as iteration.

So, let’s print 5 (from 1 to 5) numbers using “for” loop now and check how this works. Create a script file "forloop1.sh" using "vi" or any other editor of your choice as shown below:

Now, let's run the script file and see what it prints:

Practice Example 2:

--------------------------

Likewise let’s print all file names under current working directory one by one and their attributes (like date of creation, size, owner etc.,) using for loop. So, the script should be like this:

Set the execute bit on this file and run the script and check what it prints on terminal. You should get to see similar output as shown here:

In the above script file "forloop2.sh" I've used the "ls" command output which in tern is file/directory names which gets passed to the "FILE" variable one-by-one in each loop, and then prints the name of those file/directory and their attributes.

Practice Example 3:

--------------------------

The “for” loop could be used to set a range of values enclosed in curly braces to be iterated as shown below:

As you could see in the above example, I've used two range of values separated each by a space. So, each range gets evaluated separately and we could specify values as shown here. Let's execute the script and see what it prints (it should print first list of values of 1..5 and second list with values from 100..105):

Practice Example 4:

--------------------------

Likewise Bash v4.0+ has inbuilt support for setting up a step value using

{ START..END..INCREMENT } syntax as demonstrated in the below snap to print range of numbers which gets incremented in each loop (example: to print only odd numbers from 1 to 10):

After creating the script, let's set the execute bit and run it. Here, 'N' would initially start from 1 and in each loop it would increment by 2 till it reaches 10 and then it terminates the loop.

The output should be like this:

Usage of “three-expression for loop”

This format is inherited from popular “C” language style which is “initialization”, “testing”, “counting expression” as shown in the below syntax:

for (( EXP1; EXP2: EXP3 ))

do

command1

command2

…

done

In the above syntax, EXP1 would do the loop initialization (gets executed only once during the beginning of loop), EXP2 would perform test evaluation and if it passes then commands inside the "do.... done" labels would get executed & EXP3 would perform numerical operation in each loop.

Practice Example 5:

--------------------------

Lets' print out numbers from 1 to 5 using this method now. So, create a script file "forloop5.sh" as shown below and execute it.

Let’s print a pyramid of numbers using “three-expression for loop” and then reverse print the pyramid. In this example, I’d print only 1..10 numbers in pyramid style as shown in the below “forloop6.sh” file:

So, the pyramid should look similar to this one:

1

1 2

1 2 3

1 2 3 4

1 2 3

1 2

1

When executed this would print the pyramid of numbers from 1..10 and from 9..1 in reverse as shown below:

Usage of “while” loop

Like “for” loop there is “while” loop which is normally used to repeat some commands until a condition is met or not met whichever suitable. The syntax of the “while” loop is as shown below:

while [ condition ] <---------- loop runs if the condition is met

do

<command1>

<command1>

…

done

Practice Example 1:

--------------------------

Let’s create a simple shell script file to print 10 numbers one by one using “while” loop. The script file "whileloop1.sh" should be as shown below:

PageViews

About Me.

This is just about me, my experience, my interest, my passion.... I am from Mysuru which is so popularly known as Cultural Capital of Karnataka(India). I started my carrier as a technical support person, my interest grew on Linux, started working on Linux operating system, currently working as Infrastructure Lead in a private company. Although I got enough experience in this Linux world I still feel there is some empty space which needs to be filled up every day, so, every day there is a new learning, that is what we say, learning is a never ending path. We are all here to share/learn/spread the knowledge. There is much more to learn/gain in this open-source world. Happy Learning.....