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

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

What is Linux Shell Scripting for DevOps?

Shell Scripting is a way to automate tasks in a Linux operating system. It is a sequence of code or text that contains commands as input from users and execute them. Shell Scripting is a program to write a series of commands for the shell to execute. A shell script is usually created for command sequences that a user needs to use repeatedly to save time.

What is #!/bin/bash?

#!/bin/bash is the first line of the script and is called Shebang. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

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

Yes, we can use #!/bin/sh to specify the path to the Bourne Shell (sh) interpreter.

The Bourne Shell is another commonly used shell in Unix-based operating systems.

Write a Shell Script that prints “I will complete #90DaysofDeVops challenge"

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

Save the above code in a file with a .sh extension, such as devops_challenge.sh. To run the script, open a terminal, navigate to the directory where you saved the file, and execute the following command

bash devops_challenge.sh

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

#!/bin/bash

# Taking user input
echo "Enter your name:"
read name

# Taking input from command-line arguments
age=$1

# Printing the variables
echo "Name: $name"
echo "Age: $age"

Save the above code in a file with a .sh extension, such as user_input.sh. To run the script, open a terminal, navigate to the directory where you saved the file, and execute the following command

bash user_input.sh 35

In this example, the script takes user input for the name and accepts the age as a command-line argument. Replace 35 with the desired age. The script then prints the values of the name and age variables, based on the user input and the command-line argument.

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

#!/bin/bash
# Comparing two numbers
num1=10
num2=20
if [ $num1 -eq $num2 ]; then
    echo "Numbers are equal."
elif [ $num1 -gt $num2 ]; then
    echo "Number 1 is greater than Number 2."
else
    echo "Number 2 is greater than Number 1."
fi

In this example, we compare two numbers (num1 and num2) using if-else statements. The script assumes num1 to be 10 and num2 to be 20, but you can modify the values as per your requirement.

The -eq operator checks if the two numbers are equal, while the -gt operator checks if the first number is greater than the second number. Depending on the comparison result, the script prints the corresponding message using echo.

To run the script, save it in a file with a .sh extension, such as compare_numbers.sh. Open a terminal, navigate to the directory where you saved the file, and execute the following command:

bash compare_numbers.sh

Thank you for reading!

Contact me at :

linkedin: https://www.linkedin.com/in/sutish-pradhan/

E-mail: psutish@gmail.com