Skip to main content

Command Palette

Search for a command to run...

Day4:Linux Shell Scripting for DevOps Engineers

Day4 : Basics of Shell Scripting #90DaysofDevops

Updated
3 min read
Day4:Linux Shell Scripting for DevOps Engineers
S

Experienced AWS DevOps Engineer with 9+ years in IT, specializing in AWS for over 5 years. Holder of 5 AWS certifications, adept at optimizing infrastructure and driving efficiency.

  1. Kernel :

    The Kernel is the central component of an operating system that oversees the computer's operations and hardware. Its primary function is to manage memory and CPU time, serving as the core of the operating system. The Kernel acts as a crucial link between applications and data processing at the hardware level, facilitating communication between processes and system calls.

When an operating system is loaded, the Kernel is the first to load into memory and remains there until the operating system is shut down. It plays a pivotal role in tasks such as disk management, task coordination, and memory management.

  1. Shell :

    A shell is a specialized user program that provides an interface for users to interact with operating system services. The shell accepts human-readable commands from users and translates them into instructions that the Kernel can comprehend. It functions as a command language interpreter, executing commands received from input devices like keyboards or files. The shell is initiated when a user logs in or starts the terminal.

  2. What is Shell Scripting?:

    Shell Scripting is a programming method to write a series of commands.

Shell scripting holds significant importance in process automation on Linux, allowing users to automate various tasks. Users can write a sequence of commands in a file and execute them, saving time by consolidating regularly used commands into one file. This approach enhances efficiency in performing daily tasks and enables users to schedule automatic task execution using crontab.

  1. What is #!/bin/bash? Can we write #!/bin/sh as well? :

    Any shell script is identified by a Shebang.

A Shebang is a combination of bash (#) and bang (!), followed by the shell path. Placed at the beginning of a script, the Shebang informs the system that the file contains commands to be interpreted by the command interpreter.

The #! is a two-byte magic number, indicating the file type, in this case, an executable shell script. While #!/bin/bash is commonly used, you can also use #!/bin/sh, representing the original version of the shell. Various types of shell scripts exist, with some commonly used ones listed below:

  • #!/bin/sh - Bourne Shell (original version)

  • #!/bin/ksh - Korn Shell

  • #!/bin/bash - Bourne Again Shell (most widely used)

  1. Write a Shell Script which prints I will complete

    #90DaysOfDevOps challenge :

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

Execution:

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

#!/bin/bash
# Taking user input for the first number
echo "Enter the first number:"
read num1

# Taking user input for the second number
echo "Enter the second number:"
read num2

# Adding the two numbers
sum=$((num1 + num2))

# Printing the result
echo "The sum of $num1 and $num2 is: $sum"

Execution:

  1. Write a Shell Script comparing 2 numbers. :

#!/bin/bash

# Taking user input for the first number
echo "Enter the first number:"
read num1

# Taking user input for the second number
echo "Enter the second number:"
read num2

# Comparing the two numbers
if [ $num1 -eq $num2 ]; then
  echo "The 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

Execution:

Thank you for reading! Hope you find this helpful. #day04challenge #90daysofdevops

Always open to suggestions!

~ Suresh Kumar 🙂