Tuesday, 27 June 2017

Bash Scripting - 'until' Loop

Bash 'until' loop - We have been discussing loops in shell scripting and so far, we have covered two of its kinds - for loop and while loop, in two of our recent articles. In this article, we will be covering another type of a loop - until loop. If you have already read about while in bash scripting, you will find that until loop behaves exactly opposite to that of while loop. How, lets see it.

bash-until-loop

The 'until' Loop

In until loop, just the same as while loop, we have a condition, which is checked after every iteration. But the difference is here, which makes until loop's behavior exactly opposite to the while loop. In until loop,as long as the condition stays False, the block of code keeps executing, thus forming a loop. As soon as the condition becomes True, the loop is terminated.

Syntax:

until [ Condition ]
do

    -- Block of Commands --

done

Example:

#!/bin/bash

n=1

until [ $n -gt 5 ]
do
    echo "This is Iteration No. $n"
    n=$( expr $n + 1 )
done

In above code, we have n=1 before we enter the loop. When we enter the loop, the condition is checked whether n <= 5, which is False, and we enter the loop. Now, echo statement displays current value of n which is 1. Then, n is incremented by 1 (Please check our article on Arithmetic Operations for more details about expr). When the block ends, we have n=2 and the control goes back to until, where the condition is checked again. Condition follows, echo prints the value of n and increments its value by 1. So, after 5 iterations, we would have echo statement executed 5 times and with n incremented to value 6. At this point, control goes to until, condition is checked and evaluated to be True. Hence the loop exits here. In the end, we have below result in the output.

This is Iteration No. 1
This is Iteration No. 2
This is Iteration No. 3
This is Iteration No. 4
This is Iteration No. 5

An Infinite 'until' Loop

An infinite until loop is the one, which never ends. So, for this, you can mention an expression that evaluates to False. The condition always being False, the block of code will keep executing infinitely, without exiting.

Example:

#!/bin/bash

until [ 5 -ne 5 ]
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

'break' Statement

The break statements are used in the loops to exit from the loop prematurely, based on certain condition (Please take look over our article on Bash Conditionals). If the condition follows, break statement is executed, which then prevents further iterations from happening and exits from loop.

Syntax:

until [ condition ]
do
    -- Some Commands --

    if [ condition ]
    then
        break
    fi

    -- More commands --

done

Example:

#!/bin/bash

until [ 5 -gt 5 ]
do
    echo "Enter a single-digit number :"
    read n

    if [ $n -gt 9 ]
    then
        echo "Wrong Entry! Program will terminate now.."
        break
    fi
    
    echo "Good!"
done

Output:

Enter a single-digit number :                                                                                                                                            
1                                                                                                                                                                        
Good!                                                                                                                                                                    
Enter a single-digit number :                                                                                                                                            
9                                                                                                                                                                        
Good!                                                                                                                                                                    
Enter a single-digit number :                                                                                                                                            
10                                                                                                                                                                       
Wrong Entry! Program will terminate now..

'continue' Statement

The continue statement in a loop, when certain condition becomes true, skips all the subsequent statements, coming after it, and continues with the next iteration of the loop. So, when a continue statement is reached, further portion of code is skipped from execution and next iteration is started.

Syntax:

until [ condition ]
do
    -- Some Commands --

    if [ condition ]
    then
        continue
    fi

    -- More commands --

done

Example:

We take an example of printing all even numbers less than 20. In this case, we check whether a number is perfectly divisible by 2, using mod operator. If it is not, its clearly an odd number, so we prefer not to print it and skip further processing. For this, we introduce a continue statement in this condition, to prevent odd numbers from printing.

#!/bin/bash

i=0
until [ $i -gt 20 ]
do
    i=$(expr $i + 1)
    j=$(expr $i % 2)

    if [ $j -ne 0 ]
    then
        continue
    fi

    echo "$i"
done

Output:

2                                                                                                                                                                        
4                                                                                                                                                                        
6                                                                                                                                                                        
8                                                                                                                                                                        
10                                                                                                                                                                       
12                                                                                                                                                                       
14                                                                                                                                                                       
16                                                                                                                                                                       
18                                                                                                                                                                       
20

Exercise: Sum of squares of first 'N' natural numbers

#!/bin/bash

echo "Enter the Last Number"
read n

i=1
sum=0

until [ $i -gt $n ]
do
    j=$(expr $i \* $i)
    sum=$(expr $sum + $j)
    i=$(expr $i + 1)
done

echo "Sum of Squares of First $n Natural Numbers = $sum"

Result:

Enter the Last Number
6
Sum of Squares of First 6 Natural Numbers = 91

With this, we have come to an end of this discussion on bash until loops. In this article, we've learned how until are constructed and how the control flow works. We have also revised how break and continue statements work in until loops. Please share your views and feedback in the comments section below and stay tuned. Thank you.

Python - Insertion Sort Algorithm Implementation

Insertion sort algorithm - In the last couple of articles, we studied Bubble sort and Selection sort algorithms. In this article, we will learn about another sorting algorithm - Insertion sort. Just like Bubble sort and selection sort, time complexity of this algorithm is O(n^2) and space complexity being O(1).


Selection Sort Algorithm


1. First element i.e. element at index 0 is always sorted and is the part of sorted sub-list
2. Select next element
3. Compare it with all elements in sorted sub-list
4. If the value of the element is smaller, swap it with the element(s) in sorted sub-list
5. Continue till we get an element from sorted sub-list which is smaller than the element
6. Insert the element in the sorted sub-list
7. Get the next element
7. Repeat this process for selected element

Example


Consider the following list-


Initially, the first element in the list is already sorted. Hence we add that to sorted sub-list identified with green color.


Iteration 1:
Compare element at index 1 with the elements in sorted sub-list and insert at appropriate location. Here, array[0] > array[1], so array[1] should appear before array[0]. Hence we swap them to adjust their positions.


Iteration 2:
In this case, array[2] > array[1], hence nothing needs to be required as it is already at appropriate position.


Iteration 3:
In this case, array[3] < array[2], so we swap them so that array[3]=7 and array[2]=1. Now, array[2] which is 1 is again less than array[1], so we swap them again such that array[1]=1 and array[2]=5. Now also, array[1] > array[0], so we swap these elements such that array[0]=1 and array[1]=3 and we are done.


Iteration 4:


Iteration 5:


Iteration 6:


Iteration 7:


Python Implementation:


def insertion_sort(array):
    
    n = len(array)
    
    for i in range(1, n):
        j = i
        current = array[j]
        while j > 0 and array[j-1] > current:
            array[j] = array[j-1]
            j -= 1
        array[j] = current
        

my_array = [5, 3, 7, 1,4, 8, 2, 6]
insertion_sort(my_array)
print my_array

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Monday, 26 June 2017

Python - Selection Sort Algorithm Implementation

Selection sort algorithm - In this article, we will learn about another sorting algorithm - Selection sort. Just like Bubble sort, time complexity of this algorithm is O(n^2) and space complexity being O(1).


Selection Sort Algorithm


i = 0

while i < length(array)
begin
    search for minimum element in the list array[i:]
    exchange the minimum element with array[i]
    increment i by 1
end

Example


Consider the following list-


Iteration 1:


Iteration 2:


Iteration 3:


Iteration 4:


Iteration 5:


Iteration 6:


Iteration 7:


Iteration 8:


Python Implementation:


def selection_sort(array):
    n = len(array)
    
    for i in range(n):
        
        # Index of smallest element
        min_index = i
        
        for j in range(i+1, n):
            if array[j] < array[min_index]:
                min_index = j
                
        array[min_index], array[i] = array[i], array[min_index]
        
my_array = [5, 3, 7, 1, 4, 8, 2, 6]
selection_sort(my_array)
print my_array

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Python - Bubble Sort Algorithm Implementation

Bubble sort algorithm - In this article, we will learn about one of the sorting algorithms - Bubble sort. Time complexity of this algorithm is O(n^2) and space complexity being O(1).


Bubble Sort Algorithm


for i = 0 to [length(array) - 1]
begin
    # To keep track of whether a swap has happened
    flag = 0

    # Last 'i' elements will always be sorted
    for j = 0 to [length(array) - i - 1]
    begin
        
        # Check if array[j] > array[j+1], Swap if True
        if array[j] > array[j+1]
        begin
            swap array[j] and array[j+1]
            flag = 1
        endif
    endfor

    if flag == 0
    begin
        break
    endif

endfor

return array

Example


Consider the following list-


Here, length of the array is 8, hence there outer loop, which we will refer to as a pass, will be executed 8 times, while inner loop will be executed (8 - 0 - 1) times. Lets see the first pass.

Pass 1-1:

i = 0, j = 0
array[0] = 5, array[0+1] = 3
array[0] > array[1]
Result - 5 and 3 will be swapped.


Pass 1-2:

i = 0, j = 1
array[1] = 5, array[1+1] = 7
array[1] < array[2]
Result - Nothing happens


Pass 1-3:

i = 0, j = 2
array[2] = 7, array[2+1] = 1
array[2] > array[3]
Result - 7 and 1 will be swapped


Pass 1-4:

i = 0, j = 3
array[3] = 7, array[3+1] = 4
array[3] > array[4]
Result - 7 and 4 will be swapped

 

Pass 1-5:

i = 0, j = 4
array[4] = 7, array[4+1] = 8
array[4] < array[5]
Result - Nothing happens


Pass 1-6:

i = 0, j = 5
array[5] = 8, array[5+1] = 2
array[5] > array[6]
Result - 8 and 2 will be swapped


Pass 1-7:

i = 0, j = 6
array[6] = 8, array[6+1] = 6
array[7] > array[7]
Result - 8 and 6 will be swapped


Observation - After once complete pass, it can be observed that, the largest element in the array is bubbled up to the last position in the array. In another words, after one pass, one element in the array is sorted. Also, if no swap happens, it can be concluded that the array is sorted and we can break out of the loop. To keep track of this, we have introduced variable flag in the algorithm.

Similarly, after subsequent passes, array will look like -


After the 5th pass, there will be no swapping. Hence there is no need for more iterations and the process can be stopped.

Python Implementation:

def bubble_sort(array):
    n = len(array)
    
    for i in range(n):
        flag = 0
        
        for j in range(n-i-1):
            if array[j] > array[j+1]:
                array[j], array[j+1] = array[j+1], array[j]
                flag = 1
                
        if flag == 0:
            break

my_array = [5, 3, 7, 1, 4, 8, 2, 6]
bubble_sort(my_array)
print my_array

Output:

[1, 2, 3, 4, 5, 6, 7, 8]


Thursday, 23 February 2017

Puppet Resources and Types

Resources in Puppet Configuration - In the last article on Puppet - "How to Install and Configure Puppet Master and Puppet Agent in Linux", we learned to create and configure Puppet master and Puppet agent node. In this article, we will be learning about the very basic component of Puppet configuration, known as Resources.

puppet-resources-and-types

What are Resources?

As mentioned, Resources are the fundamental components of a system, which have certain Attributes - state it can be in (present or absent, enabled or disabled, etc.) or an action associated with it (create, delete or modify). With these resources, we can have control on system parameters (like files, users, packages, services, etc.).

Depending on what system parameter a resource can manipulate and configuration it controls, each resource belongs to a particular Resource Type. The complete list of the resource types is provided here. In this article, we will see some of the basic resource types used in writing a Puppet code (also known as Manifest).

1. file

This resource type is used to manage files, directories and symbolic links (Read more about symbolic links). As I mentioned, every resource has a set of attributes associated with it. So lets check out come important attributes associated with a file resource.

  • path - It states the absolute path of the file/directory.
  • Example - '/usr/local/nagios/etc/nagios.cfg'
  • ensure - It states the existence of a file on the system i.e. it is present or absent. If it is present, whether it is a file, directory or a link. If it is a link, one should also mention about the target attribute, representing the source file it must point to.
  • content - This attribute represent what text the file will contain. In this case, you can mention the content within single quotes, like 'Hello world!' or you can use the source attribute, which expects a path to a file. So that, both these files will have same contents.
  • mode - This attributes represents the permissions on the file in octal notations, like 0755, 0644, etc.
  • owner and group - These attributes are associated with ownership of the file.
  • replace - Valid values for this attributes are true, false, yes and no. It states whether the file should be replaced if already present or otherwise.
  • source - As mentioned above, this attribute expects a path to a file, on the same system or the Puppet master. Thus, this file will have the same contents as that of the source file mentioned.

2. package

This resource type is used to manage packages on the Puppet agent nodes, including installation, updation and removal. Lets see some of the attributes associated with this resource type.

  • name - Name of the package.
  • Example - 'httpd'
  • ensure - It states the existence of a package on the system i.e. it should be present or absent.
  • provider - This attribute represent what backend/package manager, like yum or apt or zypper, it should use to install/remove the package.
  • source - This attribute is used to represent where to find a package, in case a package is not provided by the provider and available in a remote central repository.

3. service

This resource type is used to manage services on the Puppet agent nodes. It includes starting, stopping and restarting the service. Not only this, to add or remove the service from startup. Lets see some of the attributes associated with this resource type.

  • name - Name of the service.
  • Example - 'sshd'
  • ensure - It denotes the state of a service on the system i.e. it should be running or stopped.
  • enable - This attribute represents whether a service should start at system boot or not. So, the valid values are true and false.
  • path - With this attribute, we can specify the path where Puppet can find the init scripts.

4. user

This resource type is used to manage system users on the Puppet agent nodes. It includes addition, deletion and modification of the users. Lets see some of the attributes associated with this resource type.

  • name - Name of the user.
  • Example - 'nagios'
  • ensure - It denotes the state of a user on the system i.e. it should be present or absent.
  • comment - This attribute represents description of the user, like 'Nagios Admin User'.
  • expiry - With this attribute, we can specify the date, in YYYY-MM-DD format, when the user account expires.
  • uid/gid - UID and GID of the user.
  • shell - Shell of the user, like '/bin/bash', '/bin/ksh', '/sbin/nologin', etc.

5. exec

This resource type is used to execute system commands on the Puppet agent nodes, provided that, the command should be Idempotent (meaning that, it should always produce same result when executed multiple times). Lets see some of the attributes associated with this resource type.

  • command - Command name.
  • Example - '/bin/mkdir'
  • cwd - Name of the directory from which command should be executed. If directory does not exist, command execution will fail.
  • creates - File that should be looked for before executing the command. If file doesn't exist, command will be executed and file will be created.
  • onlyif - With this attribute, we can set dependency of the executing based on exit status of another command. So, if the other command executes successfully, this command will be executed, otherwise not.
  • path - The path that should be searched to find the command.
  • returns - Exit status of the command which should be returned, must be an integer between 0-255.
  • timeout - The maximum time the command should take for completing the execution.
  • unless - Opposite of onlyif. The command will be executed if exit status of other command is non-zero.

That's it for this article, which was supposed to introduce you with Puppet Resources. In the next article, we will learn about another new concept - Puppet Manifests. Till then, stay tuned!

Wednesday, 22 February 2017

Bash Scripting - 'select' Loop and 'select-case' Statement

Bash 'select' loop - We have been learning about loops in shell scripting and in the recent articles, we have covered two of its kinds - for loop and while loop. In this article, we will be covering another type of a loop - select loop.

bash-select-loop-and-select-case-statement

Similar to one of the cases in for loop, select loop accepts a sequence of strings/characters separated by whitespace. This sequence of strings is provided as numbered options, just like multiple choice questions, on the terminal screen. You have to select from these options and corresponding command will be run. Lets learn this command in more details.

Basic 'select' Loop

In select loop, we have the similar syntax as that of for loop. As mentioned above, select loop accepts a sequence to iterate through it and present it as a numbered menu. For this, we use the in keywordas shown in below syntax.

Syntax:

select variableName in choice1 choice2 ... choiceN
do

    -- Block of Commands --
   
done

Example:

#!/bin/bash

echo "Which is Your Favorite Linux Distribution..?"

select os in Ubuntu LinuxMint CentOS RedHat Fedora
do
    echo "I also like $os !" 
done

In above example, we have provided five strings in the sequence. So, we expect the select to provide these stings in the numbered menu. We then have to select from the choices, one at a time. With each selection, the variable $os will be assigned with the value of the choice we make and echo statement will be executed.

Result:

Which is Your Favorite Linux Distribution..?                                                                                                                             
1) Ubuntu                                                                                                                                                                
2) LinuxMint                                                                                                                                                             
3) CentOS                                                                                                                                                                
4) RedHat                                                                                                                                                                
5) Fedora                                                                                                                                                                
#? 1                                                                                                                                                                     
I also like Ubuntu !                                                                                                                                                     
#? 2                                                                                                                                                                     
I also like LinuxMint !                                                                                                                                                  
#? 3                                                                                                                                                                     
I also like CentOS !                                                                                                                                                     
#? 4                                                                                                                                                                     
I also like RedHat !                                                                                                                                                     
#? 5                                                                                                                                                                     
I also like Fedora !                                                                                                                                                     
#? 6                                                                                                                                                                     
I also like  !                                                                                                                                                           
#? ^C

From above result, we have 3 observations-
  1. The loop is infinite, it will stop only when you press 'Ctrl + C'.
  2. When I entered '6', which is not a valid choice, it will still print something on the screen.
  3. The default prompt is #?, which does not make any sense.
The first two issues can be taken care of with the use of case statement in the select loop. The provision of default value in the case statement comes to the rescue here. We will not cover the case statement here, please check our article on case statement in bash scripting for that. For now, we introduce a case statement in our select loop.

Example - With case statement :

#!/bin/bash

echo "Which Operating System Do You Use..?"

select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP Mac
do
    case $os in
        "Ubuntu" | "LinuxMint")
            echo "I also use $os ..!"
            ;;

        "Windows8" | "Windows7" | "WindowsXP")
            echo "Why don't you try Linux..?"
            ;;

        "Mac")
            echo "You must be Very Rich..!"
            ;;
        *)
            echo "Invalid option. Program will exit now."
            break
            ;;
    esac
done 

Output :

Which Operating System Do You Use..?                                                                                                                                     
1) Ubuntu     3) Windows8   5) WindowsXP                                                                                                                                 
2) LinuxMint  4) Windows7   6) Mac                                                                                                                                       
#? 1                                                                                                                                                                     
I also use Ubuntu ..!                                                                                                                                                    
#? 2                                                                                                                                                                     
I also use LinuxMint ..!                                                                                                                                                 
#? 3                                                                                                                                                                     
Why don't you try Linux..?                                                                                                                                               
#? 4                                                                                                                                                                     
Why don't you try Linux..?                                                                                                                                               
#? 5                                                                                                                                                                     
Why don't you try Linux..?                                                                                                                                               
#? 6                                                                                                                                                                     
You must be Very Rich..!                                                                                                                                                 
#? 7                                                                                                                                                                     
Invalid option. Program will exit now.

So, we have dealt with two of the issues we observed earlier. Now, for the third one, we set a shell variable PS3 to the value we desire to appear as the prompt, as below.

Example - With the custom prompt :

#!/bin/bash

echo "Which Operating System Do You Use..?"

PS3="Enter your choice (must be a number): "

select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP Mac
do
    case $os in
        "Ubuntu" | "LinuxMint")
            echo "I also use $os ..!"
            ;;

        "Windows8" | "Windows7" | "WindowsXP")
            echo "Why don't you try Linux..?"
            ;;

        "Mac")
            echo "You must be Very Rich..!"
            ;;
        *)
            echo "Invalid option. Program will exit now."
            break
            ;;
    esac
done 

Output:

Which Operating System Do You Use..?                                                                                                                                     
1) Ubuntu     3) Windows8   5) WindowsXP                                                                                                                                 
2) LinuxMint  4) Windows7   6) Mac                                                                                                                                       
Enter your choice (must be a number): 1                                                                                                                                  
I also use Ubuntu ..!                                                                                                                                                    
Enter your choice (must be a number): 4                                                                                                                                  
Why don't you try Linux..?                                                                                                                                               
Enter your choice (must be a number): 6                                                                                                                                  
You must be Very Rich..!                                                                                                                                                 
Enter your choice (must be a number): 9                                                                                                                                  
Invalid option. Program will exit now.

With this, we close our discussion on select loop. In this article, we learned how a basic select loop is constructed, we observed the issues with the basic usage, then to mitigate those we introduced case statement in the loop. We also learned how we can use PS3 variable to set a custom prompt to add values to our program. This is it for this article and stay tuned for more ones. Thanks for stopping by.