Easy Shell Scripting | Linux

Easy Shell Scripting | Linux

Whenever we are interacting with any OS, we are indirectly interacting with Shell. Lets have a lookout on shell and shell scripting in this blog.

·

5 min read

Linux operating system has Hardware, Kernel, Shell and Application/Utilities as layers of architecture.

Kernel and Shell

The kernel is the core component of the OS. In layman's terms, it means Kernel has complete control over everything in the system.

Shell is a user program that acts as an interface between the user and operating systems, Kernel in special. Shell accepts user commands which are human-readable and converts them into a language that the Kernel can understand.

Shell can be of two types: Command Line Shell and Graphical Shell.

Command Line Shell is called a Terminal in Linux OS and Command Prompt in Windows OS.

Graphical Shell provides Graphical User Interface for the user who interacts with the program. There is no need to type a command for every action.

For Linux, there are many shells available like:

  1. BASH (Bourne Against Shell)

  2. CSH (C Shell)

  3. KSH (Korn Shell)

All different types of shells do the job but have different commands and different built-in functions.

We would need to execute the shell commands regularly and at times the many commands need to be executed in bulk and they can be repetitive at times. This is when Shell Scripts come into the picture.

Shell Scripts

Shell Scripts are files that have commands that are taken as input by the shell, and it executes them to avoid repetitive work.

For easy understanding, Shell scripts in Linux = Batch File in Windows.

The file extension for shell scripts is .sh

Why Shell Scripts?

  • For automating repetitive tasks.

  • They can be used for routine backups and system monitoring.

  • Easier to write and debug.

While there are many pros and cons involved with shell scripting, let us first start with few easy scripts and learn how to make shell scripting easy.

Shell Scripting of DevOps

As a DevOps Engineer/System Admin taking routine backups, monitoring systems comes in daily activities. The whole point of Shell Scripting conveys the same.

What is #!/bin/bash? Can we write #!/bin/sh as well?

#!/bin/bash is also called Shebang. Shebang is a combination of characters and/or letters that consist of a number sign and exclamation mark, that is (#!) at the beginning of a script.

A shell script has many UNIX commands, where users need to mention which shell they need to execute every time they want to run the scripts. To specify this Shebang is used. #!/bin/bash is used at the top/start of the script to tell our system to use Bash as the default shell.

Now the question arises if we can use different shebangs. Yes, we can. Some of the different shebangs are #!/bin/sh, #!/bin/csh, #!/usr/bin/php etc. Different shebangs are used for different purposes.

We can write #!/bin/sh as well. This is used to run the file using sh, which is a Bourne shell.

The main difference between the two shebangs is that #!/bin/bash instructs OS to use Bash as the command interpreter whereas #!/bin/sh instructs the internal system shell to start interpreting scripts.

Write a Shell Script that prints I will complete the #90DaysOofDevOps challenge!

Shell Scripts consists of Shell keywords, Shell commands, Functions and control flow.

  1. First, create and open a file having .sh as the file format.

     vi challenge.sh
    
  2. All scripts start with the Shebang.

  3. Use the echo command to write the desired string output.

    The shell script should look like this:

     #!/bin/bash
     echo "I will complete the #90DaysOofDevOps challenge!"
    
  4. Save and exit the file.

  5. Set the executable permission by using chmod command.

     chmod +x challenge.sh
    
  6. To execute a shell script in Linux, we use ./<filename>.sh.

     ./challenge.sh
    

The output should match your desired output.

Write a Shell Script to take user input, input from arguments and print the variables.

Shell scripts can take user inputs and incorporate them into the executable. They can also take input from the arguments.

Some arguments are:

$0 - The name of the script

$1 - The first argument sent to the script

$2 - The second argument sent to the script

$3 - The third argument... and so forth

$# - The number of arguments provided

$@ - A list of all arguments provided

An easy example of the above demand can be:

Let the filename be argexample.sh

#!/bin/bash 
echo "Username: $1"
echo "Age: $2"
echo "The username of the user is $1 who is of age $2"

To execute this script you should run it as below:

./argexample.sh

The output would like:

Username: DevOps

Age: 25

The username of the user is DevOps who is of age 25.

Write an Example of If else in Shell Scripting by comparing 2 numbers.

For comparison of two variables, we generally use the If else function.

The example can be of two numbers and compare which is greater:

#!/bin/bash
var1=1
var2=2
if [ $var1 -gt $var2 ]
    then
        echo "$var1 is greater than $var2"
    else
        echo "$var2 is greater than $var1"
fi

How was Shell Scripting?

Shell Scripting is easy and hard both. Depends on the use cases and our hands-on experience.

As we go ahead and practice regularly, shell scripting will become easy and can save a good amount of time and energy which can be utilized for other important productive tasks.

Do let me know how was your first experience with Shell Scripting.

Did you find this article valuable?

Support Sneha K S by becoming a sponsor. Any amount is appreciated!