Day 4 - Basic Linux Shell Scripting

Day 4 - Basic Linux Shell Scripting

So, before going ahead with "Shell scripting", we have to get familiar with the below terminologies.

What is Kernel?

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system. It is responsible for managing hardware resources such as the CPU, memory, and input/output devices, as well as providing services like process scheduling, memory management, and device drivers.

What is Shell?

A shell is a command-line interface that enables users to interact with an operating system. Shell accepts human-readable commands from users and converts them into something which the kernel can understand.

The most common shell used in Linux and Unix-based operating systems is the Bourne-Again Shell (bash), which is a free software command-line interface designed for Unix-like operating systems.

There are several types of shells, including:

  1. Bourne Again Shell (bash)

  2. C Shell (csh)

  3. Korn Shell (ksh)

  4. Z Shell (zsh)

Each type of shell has its own set of features, syntax, and capabilities. Some shells are better suited for specific tasks, while others offer more advanced features for power users.

What is 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.

What is Shell Scripting for DevOps?

Shell scripting is an important part of process automation in Linux. Scripting helps you write a sequence of commands in a file and then execute them.

Shell scripting is a valuable skill for DevOps engineers, as it saves your time because you don't have to write certain commands again and again. You can perform daily tasks efficiently, streamline the deployment pipeline and even schedule them for automatic execution, DevOps teams can then focus on delivering high-quality software faster and with greater confidence.

What is a Shell Script?

A shell/bash script is a series of commands written in a file. These are read and executed by the bash program. The program executes line by line.

Shell scripts end with a .sh extension.

However, you can identify a shell script with a "Shebang". Shebang is a combination of bash(#) and bang(!) followed with the bash shell path i.e., #!/bin/bash. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

For executing, shell script you can write:

  1. bash filename.sh

  2. ./filename.sh

#!/bin/bash and #!/bin/sh

So, You may find any of these two in shell scripts. Let's see if they are similar or what is the difference.

bash and sh are two different shells. Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different.

A script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell.

#!/bin/sh is a symbolic link to #!/bin/bash, which means it executes the script using the Bourne shell or a compatible shell, with path /bin/sh.

First Shell Program

User input using read command

read variable -> waits for user input
read -p "Message", variable -> prints a message and waits for user input
read -s -p "Password: " password -> To read passwords from user without showing it on screen

Shell script which take input from arguments

Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1, $2, and so on. The $0 will contain the script name.

./filename.sh first_argument second_argument third_argument

Using Variables in Shell Script

If - else in Shell Scripting for comparing 2 numbers

For Loop in Shell Scripting

Conclusion

Basic Linux shell scripting is a must-have skill for DevOps engineers. By automating repetitive tasks, managing system resources, and monitoring system performance, DevOps engineers can save time and increase productivity. With some practice, anyone can learn Linux shell scripting and take their DevOps skills to the next level.

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~ Smriti Sharma✌