Basic Linux Shell Scripting for DevOps Engineers (Post-05)

ยท

5 min read

Basic Linux Shell Scripting for DevOps Engineers    (Post-05)

What is Kernel?

๐ŸŒŸ Kernel: The kernel is like the heart of a computer's operating system. It is a computer program that has full control over all the system resources and manages hardware and software interactions. ๐Ÿง ๐Ÿ’ป

What is Shell?

๐ŸŒŸ Shell: A shell is a special user program that acts as an interface between the user and the operating system services. It interprets human-readable commands and converts them into instructions that the kernel can understand and execute. ๐Ÿš๐Ÿ–ฅ๏ธ

What is Linux Shell Scripting?

๐ŸŒŸ Linux Shell Scripting: Linux shell scripting refers to the practice of writing scripts using a shell language, typically for the command-line interpreter in Linux systems. These scripts allow users to automate tasks, perform file manipulations, run programs, and display text on the terminal. ๐Ÿ“œ๐Ÿง

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

๐ŸŒŸ The #!/bin/bash and #!/bin/sh are like special codes at the start of a script. They tell the computer which interpreter to use. It's like picking a language for the computer to understand! ๐Ÿš€

  1. #!/bin/bash: This one says, "Hey, use the powerful Bash shell to run the script!" ๐Ÿ’ช Bash has lots of cool features and is great for complex tasks.

  2. #!/bin/sh: This one tells the computer to use the basic Bourne shell. It's simple and works on most systems. ๐ŸŒˆ

You can choose either #!/bin/bash for more capabilities, or #!/bin/sh for better compatibility. It's up to you! ๐Ÿ˜Š๐Ÿ‘

In short, both shebangs are valid, and you can pick the one that suits your needs and the system you're working on! ๐Ÿงโœจ

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

Here's a shell script that prints the sentence "I will complete #90DaysOfDevOps challenge" along with the output:

#!/bin/bash

# Print the message
echo "I will complete #90DaysOfDevOps challenge"

To use this script, follow these steps:

  1. Open a text editor (e.g., nano, vim, or gedit).

  2. Copy and paste the above script into the text editor.

  3. Save the file with a ".sh" extension, for example, "devops_challenge.sh".

  4. Open a terminal and navigate to the directory where you saved the script.

  5. Make the script executable with the following command: chmod +x devops_challenge.sh.

  6. Finally, run the script by typing ./devops_challenge.sh in the terminal.

When you run the script, it will print the sentence "I will complete #90DaysOfDevOps challenge" on the terminal, like this:

I will complete #90DaysOfDevOps challenge

That's it! The script has executed successfully and displayed the desired output.๐Ÿš€

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

Below is a shell script that takes user input and command-line arguments, and then prints the variables:

#!/bin/bash

# Taking user input
echo "Please enter your name:"
read user_input

# Accessing command-line arguments
argument1=$1
argument2=$2

# Printing the variables
echo "User input: $user_input"
echo "Command-line argument 1: $argument1"
echo "Command-line argument 2: $argument2"

To use this script, follow these steps:

  1. Open a text editor (e.g., nano, vim, or gedit).

  2. Copy and paste the above script into the text editor.

  3. Save the file with a ".sh" extension, for example, "input_script.sh".

  4. Open a terminal and navigate to the directory where you saved the script.

  5. Make the script executable with the following command: chmod +x input_script.sh.

  6. To run the script and provide command-line arguments, use: ./input_script.sh argument1 argument2.

  7. When you run the script, it will prompt you to enter your name. After entering your name, it will display the user input as well as the command-line arguments you provided.

For example, if you run ./input_script.sh Hello World, the output will be:

Please enter your name:
Shahaji K
User input: Shahaji K
Command-line argument 1: Hello
Command-line argument 2: World

This script allows you to interact with the script by providing user input and command-line arguments, and then displays the values of the variables in the output. ๐ŸŒŸ

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

Below is an example of a shell script that compares two numbers using an if-else statement and provides output based on the comparison:

#!/bin/bash

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

echo "Enter the second number:"
read num2

# Compare the numbers
if [ "$num1" -eq "$num2" ]; then
  echo "Both numbers are equal."
elif [ "$num1" -gt "$num2" ]; then
  echo "The first number ($num1) is greater than the second number ($num2)."
else
  echo "The second number ($num2) is greater than the first number ($num1)."
fi

To use this script, follow these steps:

  1. Open a text editor (e.g., nano, vim, or gedit).

  2. Copy and paste the above script into the text editor.

  3. Save the file with a ".sh" extension, for example, "compare_numbers.sh".

  4. Open a terminal and navigate to the directory where you saved the script.

  5. Make the script executable with the following command: chmod +x compare_numbers.sh.

  6. Finally, run the script by typing ./compare_numbers.sh in the terminal.

When you run the script, it will prompt you to enter two numbers. After entering the numbers, it will display the comparison result. Here's an example of the output for different inputs:

Example 1:

Enter the first number:
10
Enter the second number:
5
The first number (10) is greater than the second number (5).

Example 2:

Enter the first number:
7
Enter the second number:
7
Both numbers are equal.

Example 3:

Enter the first number:
2
Enter the second number:
8
The second number (8) is greater than the first number (2).

The script compares the two numbers and provides the appropriate output based on the comparison result. ๐ŸŒŸ


Thank you for giving your precious time for reading this blog/article and also follow Shahaji Kadam for more such blogs/articles.

HashTags: #90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90days #challenge #learning

ย