Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

·

7 min read

🫀What is Kernel

  • In Linux, the kernel is the core component of the operating system. It acts as an intermediary between the hardware and the software, managing system resources, providing essential services, and enabling communication between hardware and software components.

  • The kernel handles tasks such as process management, memory management, device management, and file system management.

  • For instance, in a Linux system, the kernel manages memory allocation, and scheduling of processes, and interacts with hardware devices like the CPU, memory, disks, and accessories. It also facilitates communication between user applications and hardware by providing a standardized interface through system calls.

Here's a simple example of how the kernel operates:

  1. Process Management: The kernel schedules tasks, allocates CPU time to various processes, and manages their execution. It ensures fair access to resources and handles multitasking.

  2. Memory Management: It manages system memory, allocating and deallocating memory for running processes, swapping data to disk when needed, and handling memory protection.

  3. Device Management: The kernel interacts with hardware devices such as printers, keyboards, and network cards. It handles device drivers, allowing applications to communicate with hardware without needing to know specific hardware details.

💻What is Shell

  • In Linux, a shell is a command-line interface that allows users to interact with the operating system by entering commands. It's both an interface and a scripting language that interprets user inputs and executes commands.

  • One of the most commonly used shells in Linux is the Bash shell (Bourne Again SHell). It provides features such as command-line editing, history, and job control.

Here's a perfect example:

  1. Command Execution: Users can enter commands directly into the shell prompt. For instance, typing ls and hitting Enter will list the contents of the current directory.

  2. Scripting: Shells allow users to write scripts—sequences of commands—to automate tasks. For instance, a simple Bash script to greet someone might look like this:

Bash

#!/bin/bash
echo "Hello, how are you?"

Saving this as a file (e.g., greeting.sh) and give the file permission using chmod 777 and executing it with ./greeting.sh will display Hello, how are you? in the terminal.

  1. Variables and Control Structures: Shells support variables and control structures like loops and conditionals, allowing for more complex scripting. For example:

Bash

#!/bin/bash
for i in {1..5}
do
    echo "Counting: $i"
done

This script will count from 1 to 5 and display each number.

📜What is Linux Shell Scripting?💻

  • Linux shell scripting refers to writing scripts or programs using shell commands and syntax to automate tasks, perform system administration, or execute a series of commands in sequence.

  • Shell scripts in Linux are text files containing a series of commands that the shell interprets and executes. These scripts leverage the capabilities of the shell to perform various tasks, such as file manipulation, system configuration, process management, and automation of repetitive tasks.

Key aspects of Linux shell scripting include:

  1. Automation: Shell scripts automate tasks that might otherwise require manual execution of multiple commands.

  2. Portability: Shell scripts can often run on different Linux distributions and Unix-like operating systems with minor adjustments.

For instance, a shell script could automate the backup of files, installation of software packages, or monitoring system performance.

The most common shell used for scripting in Linux is the Bash shell (Bourne Again Shell), but other shells like sh, csh, ksh, and zsh also support scripting.

Shell scripting is a powerful tool for both beginners and experienced Linux users, providing a flexible and efficient way to interact with the system and perform a wide range of tasks through automation.

Tasks

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

  • Shell scripting in DevOps involves creating automated sequences of commands for managing and streamlining software development and operations tasks.

  • These scripts use shell languages like Bash to interact with operating systems, configure environments, deploy applications, and perform routine tasks.

  • For instance, a shell script could automate the deployment of a web application by pulling code from a repository, setting up the environment, and starting the server. This enables DevOps teams to save time, reduce errors, and ensure consistency in their workflows.

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

  • #!/bin/bash is called a "shebang" and appears at the beginning of a shell script. It tells the operating system which interpreter to use for executing the script, in this case, the Bash shell.

  • Yes, you can use #!/bin/sh as well. It refers to the system's default shell interpreter. However, keep in mind that the functionalities might be limited compared to Bash. So, choosing between them depends on your script's requirements and desired features.

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

  • To create and run this script, follow these steps:

    1. Open a text editor (e.g., nano, vim, or a code editor like VSCode).

    2. Copy and paste the script into the editor.

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

    4. Give permission to that file using chmod 777 challenge_script.sh

    5. Open your terminal and navigate to the directory where the script is saved.

    6. Run the script using the command: ./challenge_script.sh.

Bash

# vim challenge_script.sh

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

Output:

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

  • To create and run this script, follow these steps:

    1. Open a text editor (e.g., nano, vim, or a code editor like VSCode).

    2. Copy and paste the script into the editor.

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

    4. Give permission to that file using chmod 777 input_script.sh

    5. Open your terminal and navigate to the directory where the script is saved.

    6. Run the script using the command: ./input_script.sh.

Bash

# vim input_script.sh

  #!/bin/bash

  # Taking user input
  echo -n "Enter your name: "
  read username

  # Using command-line argument
  arg_value=$1

  # Printing variables
  echo "User input: $username"
  echo "Argument input: $arg_value"

The script will display the provided user input and argument value:

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

  • In this example, the script compares num1 and num2 using if-else statements. It checks whether they are equal or which one is greater. The -eq flag is used for equality and -gt for greater than. The script then prints the appropriate message based on the comparison.

  • To create and run this script, follow these steps:

    1. Open a text editor (e.g., nano, vim, or a code editor like VSCode).

    2. Copy and paste the script into the editor.

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

    4. Give permission to that file using chmod 777 ifelse_script.sh

    5. Open your terminal and navigate to the directory where the script is saved.

    6. Run the script using the command: ./ifelse_script.sh.

Bash

  #!/bin/bash

  num1=10
  num2=20

  if [ "$num1" -eq "$num2" ]; then
      echo "Both numbers are equal"
  elif [ "$num1" -gt "$num2" ]; then
      echo "$num1 is greater than $num2"
  else
      echo "$num2 is greater than $num1"
  fi

Explanation of this script:

  1. #!/bin/bash: This line is called the shebang. It specifies the interpreter that will execute the script.

  2. num1=10 and num2=20: These lines assign values to two variables, num1 and num2. num1 is assigned the value 10, and num2 is assigned the value 20.

  3. if [ "$num1" -eq "$num2" ]; then: This line begins an if statement. It checks if the value of num1 is equal to the value of num2 using the -eq operator for numeric comparison. The square brackets [ ] are used for the test command in shell scripting.

  4. echo "Both numbers are equal": If the condition in the if statement is true (if num1 is equal to num2), this line will be executed, printing the message "Both numbers are equal" to the terminal.

  5. elif [ "$num1" -gt "$num2" ]; then: The elif (else if) statement allows for another condition to be checked if the first condition is false. This line checks if num1 is greater than num2 using the -gt (greater than) operator.

  6. echo "$num1 is greater than $num2": If the elif condition is true (if num1 is greater than num2), this line will be executed, printing the message "$num1 is greater than $num2" to the terminal.

  7. else: If none of the above conditions are true, the else block is executed.

  8. echo "$num2 is greater than $num1": In this case, if neither num1 is equal to num2 nor num1 is greater than num2, this line will be executed, printing the message "$num2 is greater than $num1" to the terminal.

  9. fi: This line marks the end of the if statement, closing the conditional block.

When you run the script, it will output:


Happy Learning

Thanks For Reading! :)

-DevOpsParthu💝💥