Basic Linux Shell Scripting

Basic Linux Shell Scripting

ยท

3 min read

Hello DevOps enthusiasts! Today on Day 4 of #90DaysOfDevOps, we're exploring Basic Linux Shell Scripting, a fundamental tool for automation.

Understanding the Kernel and Shell

Kernel: The core controller of an operating system.

  • The kernel is a program that has complete control over the system, acting as the essential component of the operating system.

Shell: Your interface for interacting with the system.

  • A shell is a user program that provides an interface for users to interact with the operating system. It interprets commands from users and converts them into instructions the system can understand.

Demystifying Linux Shell Scripting

What is Shell Scripting for DevOps?

  • Shell Scripting Essentials:

    • A shell script is a series of commands written for the shell (command-line interpreter).

    • It serves as a powerful tool for automating tasks within the Linux environment.

  • Role in DevOps:

    • In the context of DevOps, shell scripting is invaluable for automation and orchestration of various processes.

    • It allows DevOps engineers to streamline repetitive tasks, enhance efficiency, and maintain consistency in operations.

#!/bin/bash vs. #!/bin/sh

  • Understanding the Shebang Line:

    • The shebang line (#!/bin/bash or #!/bin/sh) at the beginning of a script specifies the shell that will execute the script.

    • #!/bin/bash points to the Bash shell, while #!/bin/sh refers to the system's default shell.

Now, Let's have a look at the tasks:

  • Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

  • 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

I hope you've given it a shot yourself; if not, feel free to take a peek!

So for the First task [Write a Shell Script which prints I will complete #90DaysOofDevOps challenge]. here's the following simple script:

#!/bin/bash

echo "I will complete #90DaysOfDevOps challenge"

Save this script in a file (e.g., pledge.sh) and make it executable using the following command:

chmod +x pledge.sh

Then, you can run the script using:

./pledge.sh

It will display the specified statement on the terminal.

Now, on to the second task[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 userName
echo "User input: $userName"

# Taking input from command-line arguments
argument1=$1
argument2=$2

echo "First command-line argument: $argument1"
echo "Second command-line argument: $argument2"

# Printing all variables
echo "All variables:"
echo "User input: $userName"
echo "First command-line argument: $argument1"
echo "Second command-line argument: $argument2"

Save this script in a file (e.g., inputScript.sh) and make it executable using:

chmod +x inputScript.sh

You can run the script and provide command-line arguments like this:

./inputScript.sh argument1 argument2

It will prompt you for user input and display the entered values along with the command-line arguments.

Now onto the last task of the day[Write an Example of If else in Shell Scripting by comparing 2 numbers]:

#!/bin/bash

# Prompt the user to enter two numbers
echo "Enter the first number:"
read num1

echo "Enter the second number:"
read num2

# Compare the two numbers
if [ "$num1" -eq "$num2" ]; then
    echo "The numbers are equal."
elif [ "$num1" -gt "$num2" ]; then
    echo "$num1 is greater than $num2."
else
    echo "$num1 is less than $num2."
fi

Save this script in a file (e.g., compareNumbers.sh) and make it executable using:

chmod +x compareNumbers.sh

When you run the script, it will prompt you to enter two numbers and then compare them, displaying the result based on the if-else conditions.

Well, that's all from my side. Stay cool, stay humble, and happy coding! ๐Ÿš€๐Ÿ˜Ž

ย