🚀 Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

📌 What is Kernal?

⚙️The Kernal is a computer program that is the core of a computer's operating system, with complete control over everything in the system.

⚙️The kernel is the first thing that starts when you turn on your computer, and it stays active as long as your computer is on, making sure everything runs smoothly and efficiently so you can use your computer to do all the things you love!


📌 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 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 start the terminal.

🐚Think of the shell as a bridge between you (the user) and the computer's operating system. It takes the commands you type and translates them into instructions that the operating system can understand and execute. It then displays the results or responses from the operating system back to you.

🐚The shell provides a powerful way to control and manage the computer, as you can perform tasks such as navigating the file system, managing files and directories, configuring system settings, and running various programs—all through text-based commands.


📌 What is Linux Shell Scripting?

🗃️Linux shell scripting refers to writing scripts or programs in a shell language to automate tasks and interact with the operating system through the command-line interface (CLI). It involves creating a series of commands and logical operations that the shell interprets and executes, allowing users to perform various tasks efficiently and consistently.

Key aspects of Linux shell scripting:

  1. Shell Languages: The most common shell language used for scripting in Linux is the Bash shell (Bourne Again Shell), which is the default on many Linux distributions. Other shell languages like Zsh and Fish are also popular and provide additional features and capabilities.

  2. Automation: Shell scripts are used to automate repetitive tasks or complex sequences of commands. This can include tasks such as file management, system configuration, data processing, backups, and more.

  3. Command Execution: Shell scripts can execute system commands and utilities, allowing users to interact with the operating system and perform operations like moving files, creating directories, managing processes, and more.

  4. Variables and Control Structures: Shell scripts use variables to store and manipulate data, as well as control structures like loops and conditional statements to create logic and decision-making capabilities.

  5. Input and Output: Shell scripts can read input from users, files, or other sources and produce output to the screen, files, or other destinations. This enables script users to provide input and receive meaningful results.

  6. Script Execution: To run a shell script, you need to make it executable and then execute it directly from the command line. Alternatively, you can specify the shell interpreter explicitly to run the script (e.g., bash script.sh).

Script Extensions: Shell scripts usually have file extensions .sh or no extension at all. The extension helps users identify the script type, but it is not mandatory.


📌 Tasks📌


👉🏼Explain in your own words and examples, what is Shell Scripting for DevOps.

  1. Shell scripting for DevOps is the practice of using shell scripts to automate various tasks and workflows in the context of DevOps practices. DevOps is a set of principles that emphasizes collaboration, communication, and automation between development and operations teams to achieve faster and more reliable software delivery.

  2. Shell scripting in DevOps allows teams to achieve faster development cycles, improved reliability, and better collaboration. However, it's essential to ensure proper error handling, security measures, and code maintainability to create robust and scalable automation scripts. Additionally, as DevOps encompasses a wide range of tools and technologies, shell scripts often complement other automation and configuration management tools, such as Ansible, Terraform, or Jenkins, in a comprehensive DevOps toolchain.


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

  • The line #!/bin/bash is called a shebang (also known as a hashbang or pound-bang). It is a special construct at the beginning of a script file that tells the operating system which interpreter should be used to execute the script. In this case, #!/bin/bash indicates that the script should be interpreted and executed using the Bash shell.

  • The key difference between #!/bin/bash and #!/bin/sh lies in the shell environment. Bash provides additional features and capabilities beyond the POSIX standard, which is the minimal set of features that all compliant shells should support. If you use#!/bin/bash, you can take advantage of Bash-specific features in your script. However, if you use#!/bin/sh, your script will stick to the basic POSIX shell features, making it more portable across different Unix-like systems.


👉🏼Write a Shell Script that prints I will complete #90DaysOofDevOps challenge

Step 1 - create a file using touch command touch 90day_challenge.sh

Step 2 - we can use Vim editor to write the logic and save the file.

Step 3 - Now we can execute a file ./90days_challenge.sh

📝Before you run the script, make sure it has execution permission. You can grant permission using the chmod command:


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

Step 1 - create a file using touch command touch input_script.sh

touch input_script.sh

Step 2 - we can use Vim editor to write the logic and save the file.

vim input_script.sh

Step 3 - Give the permission to executable.

chmod +x input_script.sh

Step 4 - Now we can execute a file ./input_script.sh


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

Step 1 - create a file using touch command touch compare_2_number.sh

touch compare_2_number.sh

Step 2 - we can use Vim editor to write the logic and save the file.

vim compare_2_number.sh

Step 3 - Give the permission to executable.

chmod +x compare_2_number.sh

Step 4 - Now we can execute a file ./compare_2_number.sh

In this example, we have two variables num1 and num2, both assigned with integer values. The script then uses an if-else statement to compare the values of num1 and num2.

  • The -gt operator checks if num1 is greater than num2.

  • The -lt operator checks if num1 is less than num2.

If num1 is greater than num2, it prints that information. If num1 is less than num2, it prints the respective message. If both numbers are equal, it will print "Both numbers are equal."

You can change the values of num1 and num2 in the script to test different comparisons.


🌟 Conclusion:

Congratulations! 🎉 Shell scripting is a method of writing scripts or programs using shell commands and scripting languages to automate tasks and interact with the operating system through the command-line interface (CLI). We hope this blog has given you a Question/Answer 🚀 Embrace the Shell power and have fun with your Shell Scripting journey! 💪

🔍 Did you find this blog helpful? Let us know in the comments below! 👇 And if you have any questions or need further assistance, we're here to help! 🤗

Happy Shell-Scripting! 💻✨