Basic Linux Shell Scripting for DevOps Engineers

Basic Linux Shell Scripting for DevOps Engineers

What is Kernel

What is Shell

What is Linux Shell Scripting?

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

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

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

Why do we need Shell Scripting📜💻?

There are many reasons to write shell scripts —💻

• To avoid repetitive work and automation📝.

• System admins use shell scripting for routine backups.

• System monitoring🔍.

• Adding new functionality to the shell etc.

What is Shell Scripting📜💻?

If you are using any major operating system you are indirectly interacting to the shell. If you are running Ubuntu, Linux Mint or any other Linux distribution, you are interacting to shell every time you use the terminal. Let's discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with the following terminologies —

🏮Kernel

🏮Shell

🏮Terminal

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 manages the following resources of the Linux system —

File management

Process management

I/0 management

Memory management

Device management etc.

What is Shell?

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 a user and converts them into something the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files.

The main difference between kernel and shell is that the kernel is the core of the operating system that controls all the tasks of the system while the shell is the interface that allows the users to communicate with the kernel.

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

#!/bin/bash or #!/bin/sh is known as ‘she- bang‘(shebang)

This derives from the concatenation of the tokens sharp (#) and bang (!). It is also called sh-bang, hashbang, poundbang or hash-piling. In computing, a she-bang is the character sequence consisting of the character's number sign and exclamation mark (#!) at the beginning of a script.

/bin/sh and /bin/bash are both Unix/Linux shell interpreters, but they have some differences in their behaviour and syntax. /bin/sh is a POSIX-compliant shell that is designed to be small and efficient, while /bin/bash is a more feature-rich shell that includes many additional features and extensions.

Write a Shell Script that prints I will complete #90DaysOofDevOps challenge 📝🖨️

In this script, the line starting with #!/bin/bash specifies that the script should be interpreted using Bash. The echo command is then used to print the message "I will complete the #90DaysOfDevOps challenge" on the terminal📜.

To make it executable use the command "./script.sh".But while executable it's showing an error like Permission denied. In the previous Blog, we see how to permit a file using the command "chmod"

To make it executable use the command "chmod +x script. sh" and then run it with "./script.sh" in the terminal. The message should appear on the terminal when executed. 🚀👩‍💻.

Taking User Input and Printing Variables 📥🔤

This Bash script prompts the user for their name, reads the input, and stores it in the "varname" variable. Then, it prints a greeting message with the user's name. 😊

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

Conclusion 🎉

I hope you enjoyed reading this blog. I have one request to all give it a try on your own with some complicated scenarios like having permutations and combinations of permissions 😂. It'll definitely be helpful in your entire career.

Subscribe to my newsletter by visiting my site and also have a look at the consolidated list of all my blogs.

Cheers