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

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

ยท

4 min read

The world of computers is a fascinating one, filled with intricate layers that work together to make our devices perform their magic. Two essential components that play a pivotal role in this digital symphony are the kernel and the shell. Let's dive into these concepts and unravel their significance in the realm of computing! ๐Ÿš€

๐ŸŒ What is a Kernel?

  • The kernel is the core of an operating system, acting as a bridge between software applications and the hardware of a computer.

  • It manages system resources, including memory, input/output devices, and CPU scheduling.

  • Think of the kernel as the operating system's brain, making critical decisions to ensure efficient communication between software and hardware.

๐Ÿš What is a Shell?

  • A shell is a user interface that enables interaction with the operating system through text-based or graphical commands.

  • It serves as an intermediary between users and the kernel, allowing users to execute various commands and manage files and processes.

  • Shells come in different flavors, offering diverse features and functionalities for user interaction.

๐Ÿ“œ Why Do We Need Shell Scripts?

Shell scripting provides a powerful way to automate tasks and streamline processes. Here's why shell scripts are a must-have in a developer's toolkit:

  • Efficiency: Automating repetitive tasks saves time and reduces the risk of errors.

  • Customization: Shell scripts can be tailored to meet specific requirements, enhancing flexibility.

  • Batch Processing: Large-scale operations can be performed seamlessly through scripts.

  • System Administration: Shell scripts simplify tasks like system monitoring, backups, and software installation.

  • Rapid Prototyping: Developers can quickly test ideas and concepts using shell scripts.

๐Ÿ” Exploring Shells: Types and Commands

There are several types of shells available, each with its unique features. To list the available shell types on your system, use the command:

cat /etc/shells

This command will display a list of shell paths that are recognized on your system.

Here are some commonly used shell types:

  • Bash (Bourne-Again Shell): The most popular Unix shell, known for its rich features and scripting capabilities. Located at /bin/bash.

  • Zsh (Z Shell): Offers advanced command-line editing and customization. Located at /bin/zsh.

  • Fish: Known for its user-friendly syntax highlighting and auto-suggestions. Located at /usr/bin/fish.

  • Sh (Bourne Shell): The original Unix shell. Located at /bin/sh.

๐Ÿ“ Location of Bash

Bash, the beloved shell for many, is typically located at /bin/bash. To find the exact path on your system, you can use the which command:

which bash

Tasks:

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

Shell scripting involves writing a set of commands in a scripting language that can be interpreted and executed by a shell, such as Bash (Bourne Again SHell), which is the most commonly used shell on Unix-like systems. Shell scripts are plain text files with a .sh extension that contain a sequence of commands, control structures, and variables.

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

  • The #!/bin/bash is called the shebang, and it's a special directive that tells the operating system which interpreter should be used to execute the script. In this case, /bin/bash is the path to the Bash interpreter.

  • Yes, we can use #!/bin/sh in the shebang line. However, it's important to note that /bin/sh may not always point to Bash specifically. It could represent a different shell, such as the Bourne shell (sh) or a compatible shell.

Write a Shell Script which printsI will complete #90DaysOofDevOps challenge

#!/bin/bash
echo "I will complete #90DaysOofDevOps challenge"

In above example, the script begins with the shebang #!/bin/bash, indicating that the Bash interpreter should be used. The echo command is used to print the desired message to the console

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

#!/bin/bash
read -p "Enter your name: " name
echo "Hello, $name! Welcome to the world of DevOps."

This script demonstrates how to accept user input using the read command. It prompts the user to enter their name, stores the input in the name variable, and then prints a personalized greeting.

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

#!/bin/bash
num1=100
num2=200
if [ $num1 -gt $num2 ]; 
then
    echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; 
then
    echo "$num1 is less than $num2"
else
    echo "$num1 is equal to $num2"
fi

In above example, two variables, num1 and num2, are defined with values 100 and 200, respectively. The script uses the if-elif-else-fi control structure to compare the values of these variables. Based on the comparison, it prints the appropriate message to the console, indicating whether num1 is greater than, less than, or equal to num2.

Conclusion

Shell scripting is an essential skill for DevOps professionals as it allows the automation of various tasks, enhances efficiency, and promotes consistency in operations. Understanding the basics of shell scripting, such as the shebang line, user input handling, variable usage, and conditional statements, empowers DevOps engineers to create powerful and reusable scripts to streamline their workflows.

Thank you for reading! Keep Supporting, learning and growing with each other.

ย