Day 4 - Basic Linux Shell Scripting for DevOps Engineers

Day 4 - Basic Linux Shell Scripting for DevOps Engineers

1.) What is Kernel

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.

2.) What is Shell

A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from a user and converts them into something that the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.

3.) What is Linux Shell Scripting

A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Tasks -

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

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

  • 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

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

Shell scripting is a programming language that is used to automate tasks and system administration in a Linux or Unix environment. In the context of DevOps, shell scripting plays a crucial role in automating repetitive tasks and streamlining the deployment and management of applications.

Here are some examples of how shell scripting can be used in DevOps:

  • Automating the deployment of new applications to production servers.

  • Configuring and managing infrastructure, such as virtual machines and networks.

  • Provisioning and configuring development environments.

  • Running tests and collecting metrics.

  • Monitoring and alerting on system health.

  • Backing up and restoring data.

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

The shebang, #!/bin/bash, is a line that is used at the beginning of a shell script. It tells the operating system which shell to use to interpret the script. In this case, the shell is bash, which is the most common shell on Linux and Unix systems.

Yes, you can also write #!/bin/sh instead of #!/bin/bash. #!/bin/sh is a more generic shebang that can be used with any shell. However, if you are writing a script that is specifically designed to be run with bash, then you should use #!/bin/bash. This is because bash has some features that are not available in other shells, such as command history and variable expansion.

Here is an example of a shell script that uses the shebang:

#!/bin/bash

# This is a simple shell script that prints "Hello, world!"

echo "Hello, world!"

3.) Write a Shell Script that prints I will complete the #90DaysOofDevOps challenge

Here is a shell script that prints "I will complete #90DaysOfDevOps challenge":

#!/bin/bash

# This script prints "I will complete #90DaysOfDevOps challenge"

echo "I will complete #90DaysOfDevOps challenge"

To run this script, save it as a file with the .sh extension, such as 90DaysOfDevOps.sh. Then, make the script executable by running the following command:

chmod +x 90DaysOfDevOps.sh

Finally, run the script by typing the following command:

./90DaysOfDevOps.sh

This will print the message "I will complete #90DaysOfDevOps challenge" to the console.

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

here is a shell script that takes user input, input from arguments and prints the variables:

#!/bin/bash

# This script takes user input and input from arguments and prints the variables

# Get the user's name
read -p "What is your name? " name

# Get the first argument
first_arg=$1

# Get the second argument
second_arg=$2

# Print the variables
echo "Your name is $name"
echo "The first argument is $first_arg"
echo "The second argument is $second_arg"

To run this script, save it as a file with the .sh extension, such as user_input.sh. Then, make the script executable by running the following command:

chmod +x user_input.sh

Finally, run the script by typing the following command:

./user_input.sh

This will prompt you to enter your name. After you enter your name, the script will print the variables name, first_arg, and second_arg.

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

Here is an example of an if-else statement in shell scripting that compares two numbers:

#!/bin/bash

# This script compares two numbers and prints the greater number

# Get the two numbers
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

# Compare the numbers
if [ $num1 -gt $num2 ]; then
  echo "The greater number is $num1"
else
  echo "The greater number is $num2"
fi

To run this script, save it as a file with the .sh extension, such as compare_numbers.sh. Then, make the script executable by running the following command:

chmod +x compare_numbers.sh

Finally, run the script by typing the following command:

./compare_numbers.sh

This will prompt you to enter two numbers. After you enter the numbers, the script will print the greater number.

Thank You

If you liked this blog then click on ❤

and do follow for more interesting and helpful blogs.

Did you find this article valuable?

Support GCP by becoming a sponsor. Any amount is appreciated!