Shell Scripting

๐Ÿ“ Linux: Introduction to Shell Scripting for DevOps๐Ÿ’ป๐Ÿ”ง

๐Ÿš€๐Ÿ’กAre you ready to be a DevOps enthusiast looking to enhance your skills in Linux shell scripting? Look no further! In this article, we'll explore the fundamentals of shell scripting and its significance in the DevOps world. Let's dive in and unravel the power of scripting for DevOps tasks. ๐Ÿš€๐Ÿ’ก

What is a Kernel? ๐ŸŒโ“

Kernel is central component of an operating system that manages operations of computer and hardware. It manages operations of memory and CPU time. It is core component of an operating system.

Kernel acts as a bridge between applications and data processing performed at hardware level using inter-process communication and system calls.

Objectives of Kernel :

  • To establish communication between user level application and hardware.

  • To decide state of incoming processes.

  • To control disk management.

  • To control memory management.

  • To control task management.

What is a Shell? ๐Ÿšโ“

A shell is a command-line interpreter that serves as a user interface to interact with the operating system. It accepts commands from users, executes them, and communicates with the kernel to perform various operations. The shell acts as a bridge between the user and the underlying system, making it easier to interact with the computer through human-readable commands.

What is Linux Shell Scripting? ๐Ÿ“œ๐Ÿ’ปโ“

A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from users and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.

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. The result is then displayed on the terminal to the user. A terminal in Ubuntu 22.04.2 LTS system looks like this โ€“

In the above screenshot โ€œlsโ€ command with โ€œ-lโ€ option is executed. It will list all the files in the current working directory in a long listing format.
Working with a command line shell is a bit difficult for beginners because itโ€™s hard to memorize so many commands. It is very powerful; it allows users to store commands in a file and execute them together. This way any repetitive task can be easily automated. These files are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.

Graphical Shells

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. Window OS or Ubuntu OS can be considered as a good example which provides GUI to the user for interacting with the program. Users do not need to type in commands for every action. A typical GUI in the Ubuntu system โ€“

There are several shells are available for Linux systems like โ€“

  • BASH (Bourne Again SHell) โ€“ It is the most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.

  • CSH (C SHell) โ€“ The C shellโ€™s syntax and its usage are very similar to the C programming language.

  • KSH (Korn SHell) โ€“ The Korn Shell was also the base for the POSIX Shell standard specifications etc.

Each shell does the same job but understands different commands and provides different built-in functions.

Tasks:

๐Ÿ”ฅExplain in your own words and examples what Shell Scripting for DevOps is. ๐Ÿ“๐Ÿ’ป

if we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal.

We can communicate with Kernerl through shell scripting, and we can execute the operation as we want. example: if we want to open one avinash.txt folder, then we simply execute a command on the terminal for shell, and it will send a response to the kernel, which will communicate with the hardware, and a file opening-related system programme will run and get output as file open.

It allows DevOps engineers to streamline processes, improve efficiency, and ensure consistency in their workflows. For example, a shell script can be used to automate the deployment of applications, configure servers, perform backups, or execute complex build processes. By automating these tasks, DevOps engineers can save time, reduce human error, and maintain better control over their environments.

๐Ÿ”ฅWhat is #!/bin/bash? Can we write #!/bin/sh as well? ๐Ÿ“œโ“

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.

Yes, it is also possible to write #!/bin/sh instead. The /bin/sh path represents the default shell on most Unix-like systems, including Linux. While sh is typically a symbolic link to another shell (often Bash or a compatible shell), using #!/bin/sh ensures portability across different systems. However, keep in mind that using #!/bin/sh limits you to the features and capabilities provided by the default shell, which might be more limited compared to Bash.

๐Ÿ”ฅWrite a Shell Script that prints "I will complete the #90DaysOfDevOps challenge." ๐Ÿ“๐Ÿ”ข

Here's an example of a shell script that achieves this:

#!/bin/bash
echo "I will complete the #90DaysOfDevOps challenge continusly."

Save the script in a file, such as challenge. sh. Make it executable using the command chmod +x challenge. sh. Then, execute the script by running ./challenge.sh. The output will be "I will complete the #90DaysOfDevOps challenge."

๐Ÿ”ฅWrite a Shell Script to take user input, input from arguments, and print the variables. ๐Ÿ“ฅ๐Ÿ“โ“

#!/bin/bash

# User input
read -p "Enter your name: " name

# Input from arguments
age=$1

# Print the variables
echo "Hello, $name! You are $age years old."

Save the script in a file, such as variables. sh, and make it executable. To execute the script, you can provide the age as a command-line argument like this: ./variables.sh 25. The script will prompt you to enter your name, and it will then display a greeting message along with the provided name and age.

๐Ÿ”ฅProvide an example of If-Else in Shell Scripting by comparing two numbers. โš–๏ธโ“

num1=10
num2=20

if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2."
elif [ $num1 -lt $num2 ]; then
    echo "$num1 is less than $num2."
else
    echo "$num1 is equal to $num2."
fi

In this script, we compare num1 and num2 using the if-else construct. The -gt operator checks if num1 is greater than num2, while the -lt operator checks if num1 is less than num2. The -eq operator is used to check for equality. Based on the comparison results, the script prints the corresponding message.

Save the script in a file, such as comparison.sh, and execute it. The output will indicate the relationship between the two numbers.

Congratulations! Now You've gained a solid understanding of basic Linux shell scripting for DevOps engineers. With these scripting skills, you can automate various tasks, improve efficiency, and unleash your potential in the DevOps world.

๐Ÿš€๐Ÿ”ง๐Ÿ”ฅ๐Ÿ’ปHappy scripting! Happy learing!๐Ÿš€๐Ÿ”ง๐Ÿ’ป๐Ÿ”ฅ

ย