Scripting Success: A Beginner's Guide to Basic Shell Scripting in Linux

Scripting Success: A Beginner's Guide to Basic Shell Scripting in Linux

Day 4: Basic Linux Shell Scripting for DevOps Engineers

Introduction

In the fast-paced world of DevOps, automation is the cornerstone of efficiency. It's the key to managing complex infrastructures, deploying applications seamlessly, and ensuring rapid, reliable delivery. Among the arsenal of tools at a DevOps engineer's disposal, shell scripting stands out as a powerful and versatile option.

What is Shell Scripting for DevOps?

In the realm of DevOps, where speed and precision reign supreme, shell scripting emerges as an indispensable tool for automating tasks within Unix-based environments. Whether you're a seasoned DevOps professional seeking to refine your skills or a newcomer eager to harness the power of automation, this guide will be your essential companion.

At its core, shell scripting involves writing a series of commands in a file (commonly known as a script) to be executed by a Unix-based shell. Think of it as a recipe for the computer, where you provide a set of instructions that it can follow step by step. This enables the automation of tasks that would otherwise be performed manually through the command line. For instance, imagine preparing a meal: you could manually chop vegetables, cook them separately, and then combine them, or you could follow a recipe that guides you through each step. Similarly, a shell script guides the computer through a sequence of actions, making tasks more efficient and less prone to error.

What is #!/bin/bash?

Okay, imagine you have a special recipe, but you need to tell the kitchen which chef should follow it. That's what #!/bin/bash happens in a computer's recipe. It says, "Hey, use the Bash chef to make sense of this!" Bash is like a super smart kitchen expert who knows exactly what to do with the recipe. So, whenever a computer sees #!/bin/bash, it knows to call Bash to understand and follow the instructions in the recipe. It's like telling the kitchen who the best chef is for the job!

Can we write #!/bin/sh as well?

Absolutely! #!/bin/sh will use the default system shell, which might also be Bash or another compatible shell. Using #!/bin/sh makes your script more portable, as it'll work on systems where Bash is not the default shell. It's like speaking a universal language in the DevOps galaxy!

So, Let's Write Some Shell Scripts!

Shell Script which prints the text "I will complete #90DaysOofDevOps challenge"

Step 1: Begin by generating a script file named filename.sh using Vim, ensuring it has a .sh extension.

Step 2: Add a shebang #!/bin/bash & appropriate command to display the desired text on the terminal within the script :

echo "I will complete #90DaysOofDevOps challenge"

Step 3: Save the script file and make it executable by executing the following command in the terminal:

chmod 777 filename.sh

Step 4: Finally, run the script in the terminal to display the intended statement:

./filename.sh

Shell Script to Take Input from Arguments and Print Variables

Step 1: Begin by creating a script file named filename.sh using vim, ensuring it has a .sh extension.

Step 2: First of all Add a shebang#!/bin/bash

Step 3: Now write arg1=$1, arg2=$2, arg3=$3: These 3 lines assign the first three command-line arguments to variables arg1, arg2, and arg3, respectively. When you run the script with arguments, these variables will store the corresponding values.

Step 4: Then write echo command to print the number of arguments through a variable that stores the number of command-line arguments passed to the script $#

echo "Number of arguments: $#"

Step 5: Now write echo the command to print the values of the variables arg1, arg2, and arg3, which contain the first three command-line arguments, respectively.

echo "Argument 1: $arg1"
echo "Argument 2: $arg2"
echo "Argument 3: $arg3"

Step 6: Save the script file and make it executable by executing the following command in the terminal:

chmod 777 filename.sh

Step 7: Finally, run the script in the terminal with 3 argument values

./filename.sh arg1 arg2 arg3

Shell Script with If-Else (e.g Comparing Two Numbers)

Step 1: Write the shebang #!/bin/bash at the beginning of the script

Step 2: Use the echo command to display messages on the screen, prompting the user to enter two numbers. The read command is then used to read the user's input and store it in the variables num1 and num2.

echo "Enter the first number:" read num1
echo "Enter the second number:" read num2
if [ $num1 > $num2 ]
then 
    echo "number1 is greater than number2"
elif [ $num1 < $num2 ]
    echo "number1 is less than number2"
else
    echo "number1 is equal to number2"
fi

we use an if-else statement to compare the values of num1 and num2. The square brackets [ ] are used for defining conditional expressions. The comparison operators used are > (greater than) and < (less than).

Step 3: Save the script file and make it executable by executing the following command in the terminal:

chmod 777 filename.sh

Note: Comments start with the # symbol in shell scripting and are ignored by the interpreter.

Step 4: Finally, run the script in the terminal.

Conclusion

Shell Scripting is a powerful tool for DevOps engineers to automate tasks and run commands efficiently. It saves time and effort by using text-based scripts. With the right scripts, you can automate processes like a breakfast ninja and focus on more important DevOps responsibilities.

Happy scripting!