Basic Linux Shell Scripting for DevOps Engineers

Basic Linux Shell Scripting for DevOps Engineers

Table of content

Introduction:
1. What is Shell?
2. What is Kernel?
3. What is Shell Scripting?
4. How to Write Shell Script in Linux

Introduction:

Shell scripting is a fundamental skill for DevOps engineers working with Linux-based systems. A shell script is a series of commands written in a scripting language that can be executed in a Unix-like shell. These scripts help automate repetitive tasks, manage system configurations, deploy applications, and streamline various processes in a DevOps environment.

1. What is Shell?

Shell is a special user program that provides an interface to user to use operating system services. Shell provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program’s output in a shell script.

What is a Kernel?

Kernel is the core component of an operating system. It serves as the bridge between software applications and the hardware of a computer system. Essentially, it's like the brain of your computer, overseeing and managing all its activities. Linux, being an open-source operating system, has a monolithic kernel that has evolved to include support for a wide range of hardware and applications.

What is Shell Scripting?

Shell scripting is a fascinating way to automate tasks and unleash the power of the command line. Basically Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute.

It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.

How to write Shell Script in Linux

Writing a shell script in Linux involves creating a text file containing a series of commands and instructions that you want the shell to execute in sequence. Below are the steps to write a basic shell script in Linux:

open a text editor program, and open a new file to begin typing a shell script or shell programming.

Then give the shell permission to execute your shell script that is it has all Read, Write, Execute permission enabled for the file or directory and put your script at the location from where the shell can find it.

Once it has all the permission, the color of file / dir has changed to green for execution.

#! is an operator called shebang which directs the script to the interpreter’s location. So, if we use #! /bin/bash the script gets directed to the Bourne Again SHell.

5. Task

Write a Shell Script which prints some message.

We will create a test1.sh file, and then use echo command to print text message.

To create a file we'll use this command:-

To run script file, following command will be used:-

./filename.sh

When you try to run the file "Permission Denied" error will come. To overcome we need to give user permission to execute the file. using chmod command.

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

Tacking Input as name and print the output

Using If-Else Statements in Shell Scripting

Let's use an if-else statement to check if a number is positive, negative, or zero.

Conclusion

Finally, shell scripting gives DevOps professionals the ability to automate, adapt, and communicate with their Linux systems. You can write effective scripts that reduce waiting time and increase productivity if you have the necessary knowledge and abilities. So, harness the power of shell scripting to open up new DevOps opportunities!

Thank you for taking the time to read this blog.

I hope you found the information helpful and insightful.