Shell Scripting

Shell Scripting

What is Shell Scripting and why shell scripting is useful for DevOps?

Day 4 of #90DaysOfDevOps with the #TrainWithShubham Community. The challenge is for the DevOps Community to get stronger in #DevOps. It is a great initiative by Shubham Londhe. Documenting my learning and also sharing it with the community is helping me to get more clear with the concepts.

What is Shell Scripting for DevOps?

The main function of the shell is to interpret or analyze Unix commands. A shell takes commands from the user and translates them into the kernel’s understandable form. In other words, it acts as a medium between the user and the kernel of the operating system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell.

Importance of Shell Scripting for DevOps

  • Shell helps in doing work which is repetitive in nature. For example: When executing a bunch of commands, often, shells can take all these commands directly from a stored file and execute it, instead of writing them again every time.

  • They are used to get routine backups by admins.

  • They are easier to write and debug than other programming languages like C or C++.

  • We can transfer the shell script to other UNIX and similar operating systems and execute it.

  • Shell scripts are also used to monitor systems regularly.

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

#!/bin/bash This is known as shebang in Unix. Shebang is a collection of characters or letters that consist of a number sign and exclamation mark, that is (#!) at the beginning of a script. We use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell.

The shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts. This system shell can vary from OS to OS(most of the time it will be bash). Whereas, when the shebang, #!/bin/sh used in scripts instructs the internal system shell to start interpreting scripts.

Some of the shebangs used for different purposes in shell scripts:-

  • #!/bin/sh - used to execute the file using sh, which is a Bourne shell, or a compatible shell.

  • #!/bin/csh - Used to execute the file using csh, the C shell.

  • #!/usr/bin/perl - Used to execute using Perl.

  • #!/usr/bin/php - Used to execute the file using PHP.

  • #!/usr/bin/python - Used to execute using Python.

Write a Shell Script which prints "I will complete #90DaysOfDevOps challenge"?

#!/bin/bash

echo "I will complete #90DaysOofDevOps challenge"

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

#!/bin/bash

echo "Welcome $1"

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

  1. The keyword if is followed by a condition.

  2. This condition is evaluated to decide which statement will be executed by the processor.

  3. If the condition evaluates to TRUE, the processor will execute the statement(s) followed by the keyword then.

  4. In a case where the condition evaluates to FALSE, the processor will execute the statement(s) followed by the keyword else.

    Command

    Description

    &&

    Logical AND

    $0

    Argument 0 i.e. the command that’s used to run the script

    $1

    The first argument (change number to access further arguments)

    -eq

    Equality check

    -ne

    Inequality check

    -lt

    Less Than

    -le

    Less Than or Equal

    -gt

    Greater Than

    -ge

    Greater Than or Equal