Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers

Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers

Shell scripting for DevOps refers to the use of shell scripts, typically written in Bash (Bourne Again SHell) or other compatible shells, to automate various tasks in the software development lifecycle, particularly in the context of DevOps practices. Shell scripting is an integral part of DevOps for automating repetitive tasks, streamlining processes, and facilitating continuous integration and continuous delivery (CI/CD) pipelines. Before understanding shell scripting we have to get familiar with the following terminologies:

  • Kernel

  • Shell

  • Terminal

Kernel

The kernel is the core component of an operating system. It serves as an intermediary between software applications and the hardware of the computer.It manages the following resources of the Linux system –

  • File management

  • Process management

  • I/O management

  • Memory management

  • Device management etc

It is often mistaken that Linus Torvalds has developed Linux OS, but actually, he is only responsible for the development of the Linux kernel.

Complete Linux system = Kernel + GNU system utilities and libraries + other management scripts + installation scripts.

Shell

The shell is a command-line interface (CLI) or a graphical user interface (GUI) through which users interact with the operating system. In the context of command-line interfaces, the shell is a program that interprets user commands and executes them by communicating with the kernel. The shell gets started when the user logs in or starts the terminal. Common shell programs include Bash (Bourne Again SHell), Zsh (Z Shell), and PowerShell (for Windows systems).

Shell is broadly classified into two categories –

  • Command Line Shell

  • Graphical shell

Command Line Shell

Shell can be accessed by users using a command line interface. A special program called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided to type in the human-readable commands such as “cat”, “ls” etc. and then it is being executed

Graphical Shell

Graphical shells provide means for manipulating programs based on the graphical user interface (GUI), by allowing for operations such as opening, closing, moving, and resizing windows, as well as switching focus between windows.

Terminal

A program which is responsible for providing an interface to a user so that he/she can access the shell. It basically allows users to enter commands and see the output of those commands in a text-based interface. Large scripts that are written to automate and perform complex tasks are executed in the terminal.

Conclusion

We learned about the essential parts of Linux systems: the kernel, which controls everything, the shell, which lets us interact with the operating system, and the terminal, our interface to give commands. We explored command line and graphical shells, like BASH, and understood that the terminal is where we type in our commands

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

#!/bin/bash is called a shebang or hashbang. It's a special notation at the beginning of a script file in Unix-like operating systems (including Linux) that tells the system which interpreter should be used to execute the script. In this case, #!/bin/bash indicates that the script should be interpreted and executed using the Bash shell.

Similarly, we can indeed write #!/bin/sh. This notation specifies that the script should be interpreted using the system's default shell, which is often a POSIX-compliant shell such as Bash, Dash, or another compatible shell.

what happens when you execute a script with a shebang line?

  1. The operating system looks at the first line of the script to find the shebang notation (#!).

  2. It then reads the path specified after the shebang (/bin/bash, /bin/sh, etc.).

  3. The operating system launches the specified interpreter (bash, sh, etc.).

  4. The interpreter reads the script file and executes its commands accordingly.

Using #!/bin/bash or #!/bin/sh at the beginning of a script is a way to ensure that the script is executed by a specific shell, which can be crucial if the script relies on shell-specific features or behaviors.

However, it's essential to note that #!/bin/bash assumes that Bash is located at /bin/bash, which might not be the case on all systems. In some environments, Bash may be located elsewhere, such as /usr/bin/bash. In such cases, you need to adjust the shebang line accordingly to point to the correct location of the Bash executable.

Similarly, #!/bin/sh relies on the system's default shell, which may vary depending on the operating system and its configuration. It's usually a POSIX-compliant shell, but it's worth verifying if you have specific requirements for shell features or compatibility.

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

Here I have created a script using vi editor and which when executed will print output "I will complete #90DaysOofDevOps challenge". In order to make the script executable the script has been given executable permission using command "chmod 700 filename". In order to execute the script you can use the command "./filename" if you are in same path as

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

#This script reads input and gives output #

!/bin/bash echo "what is your first name?"

read firstname

echo "what is your last name?"

read lastname

echo "Hello $firstname $lastname"

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

Script

#!/bin/bash

echo "Enter first number "

read num1

echo "Enter second number "

read num2

if [ $num1 -gt $num2 ]

then

echo "$num1 is greater than $num2"

else

echo "$num2 is greater than $num1"

fi

o/p when executing the command