Basic Linux Shell Scripting for DevOps Engineers

Basic Linux Shell Scripting for DevOps Engineers

What is a kernel?

A kernel can be defined as the core of the computer operating system and it has control over everything on the system.

What is shell?

Shell is a program that provides an interface to use the services provided by the operating system. Shell basically reads all the human-readable commands and converts them into commands that are readable by the kernel or understandable by the kernel. It is a command language interpreter that executes commands read from input devices such as keyboards or from files.

What is shell scripting?

Shell scripting can be defined as the series of commands which is being carried out by the shell. The shell reads out the scripts in a file and carries out all the commands in the file written in the form of a script. Shell is used for automating all the tasks and many processes that a DevOps engineer wants to perform. By automating routine tasks, shell scripts can help DevOps engineers save time, reduce errors, and increase system reliability.

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

The line "#!/bin/bash" or "#!/bin/sh" which is being used at the beginning of a shell script is called the shebang line. "#!/bin/sh" or "#!/bin/bash," tells the system which program to use to run the commands in the script. Bash is a program that can run all commands, and it's located under the "bin" directory. So, by including this line, we instruct the shell to run all the commands written in the script. Both "#!/bin/bash" and "#!/bin/sh" can be used interchangeably in a shell script.

Write a Shell Script that prints I will complete the #90DaysOofDevOps challenge.

vim: vim, which stands for "Vi Improved", is a text editor It can be used for editing any kind of text and is especially suited for editing computer programs.

Step 1: First, we will create a shell script by vim command which is named as 90_days_of_devops.sh

The shell script file must end with .sh

Step 2: Now, after creating the file we start the file with the "vim" command we will start writing our script with #!/bin/bash headline

By the command "echo" we will print "I will complete 90daysofdevops challenge ."

After writing the line which we want to print we will save the file with the "Esc" key and get out from the file with ":wq" which stands for write and quit the file.

Step 3: Now, after writing and quitting the file we will change the permissions by the command "chmod" for the file to make it readable writable and executable.

Step 4: After this we will execute the file named as "90_days_of_devops.sh" by the command "./filename".

after executing the file it will print I will complete #90DaysOfDevOps challenge.

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

Step 1: First, we will create a shell script by the "vim" command which is named user_input.sh

Step 2: Here we are using the read command which is used to declare a variable, the variable is nothing but we are storing the input in the declared variable and the rest of everything is the same.

Step 3: After saving and quitting the script, we will change the permissions of the file by the "chmod" command and make it readable writable and executable.

Step 4: Now we will execute our file user_input.sh

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

Step 1: In this, we will create a shell file by the name compare_numbers.sh

Step 2: Now in this script, we will use the if-else command to compare two numbers.

We use "-gt" to compare numbers, with it meaning greater than.

we must close the if condition with the "fi" command as shown in the screenshot.

Step 3: Now we will change the command permissions by the command "chmod".

Step 4: Now we will run the file by "./filename"

The script will prompt you to enter two numbers, and it will compare the numbers using an if-else statement.

Overall using a shell script is most useful for repetitive tasks that may be time-consuming to execute by typing one line at a time.

Thanks For Reading!

#devops #90daysofdevops #TrainWithShubham

Let's connect on Linkedin-https://www.linkedin.com/in/piyush-verma-8b1b4426a/