Day 4 Task: Basic Linux Shell Scripting for DevOps

Day 4 Task: Basic Linux Shell Scripting for DevOps

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. It manages hardware resources such as memory, CPU and i/o devices and provides essential resources for other parts of the system.

Key functions of a kernel

  1. Memory Management: Kernel manages system's memory, to allocate or deallocate memory space as required. It ensures each process has its own memory space.

  2. Process Management: It ensures that each process gets adequate CPU time and resources while maintaining the system. Kernel handles creation, scheduling and termination of processes.

What is Shell?

A shell is a CLI (command line interpreter) that provides an interface for users to interact with operating system services. It accepts human-readable commands from users and converts them into instructions that the kernel can understand.

Types of shells

  1. Command-line Interpreter: It provides a text-based interpreter that allows users to type commands. some types of cli are Bash- Bourne Again Shell, zsh- Z-Shell,

    sh- Bourne Shell.

  2. Graphical User Interface: It provides GUI- Graphical User Interface that allows users to interact with the system using graphical elements such as menus, windows and icons.

What is Linux Shell Scripting?

Linux shell scripting involves writing programs (scripts) that can be run by a Linux shell, such as bash (Bourne Again Shell). These scripts automate tasks, perform system administration tasks, and facilitate the interaction between users and the operating system. Wide range of operations performed by shell scripts include file manipulation, program execution, and printing text.

Key concepts of Shell Scripting

  1. Input/Output Redirection- Shell scripts can redirect input and output using operators like >, <, >>, and |. This allows for chaining of commands and manipulation of data streams.

  2. Error Handling- It can handle errors using techniques such as exit status, trapping signals, and conditional checks.

  3. Functions- A function is a block of code that can be defined once and called multiple times within the script. Functions in shell scripts allow for code reuse and modularity.

  4. Variables: Shell scripts can define and use variables to store and manipulate data.

Examples of Shell Scripting Use Cases

Task 1: Explain in your own words and with examples what Shell Scripting means for DevOps.

Shell scripting for DevOps is the use of scripting languages, such as Bash, zsh to automate various tasks and processes involved in the DevOps lifecycle. DevOps, which stands for Development (Dev) and Operations (Ops), is a collaborative approach that integrates software development and IT operations to streamline the software delivery process, improves the quality of software releases.

Task 2: What is #!/bin/bash? Can we write #!/bin/sh as well?

The "#!/bin/bash" line, commonly known as the shebang or hashbang, The line #!/bin/bash at the top of a script indicates the system to run the script using Bash shell, while #!/bin/sh indicates it to use the sh shell.

Key Differences

  1. Bash (#!/bin/bash):

    • Uses the Bash shell, which has many advanced features.
  2. sh (#!/bin/sh):

    • Uses the sh shell, which is more basic and widely compatible.

Task 3: Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.

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

Task 4: Write a Shell Script that takes user input, input from arguments, and prints the variables.

#!/bin/bash

# Taking user input
read -p "Enter your name: " user_name

# Taking arguments from the command line
arg1=$1
arg2=$2

# Printing the variables
echo "User Name: $user_name"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"

Task 5: Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.

#!/bin/bash

# Take two numbers as input
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

# Compare the numbers
if [ $num1 -gt $num2 ]; then
  echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
  echo "$num1 is less than $num2"
else
  echo "$num1 is equal to $num2"
fi