Basic Linux Shell Scripting for DevOps Engineers

🔹What is Kernel

The kernel is like the brain of an operating system. It is a critical component that acts as a bridge between software applications and the computer's hardware.

Imagine your computer as a complex machine with various components like the processor, memory, storage, and input/output devices. These components need to communicate with each other and with the software applications you use, such as web browsers, word processors, or games.

Here's where the kernel comes in. It provides a layer of abstraction and control over the hardware, allowing software programs to interact with the computer's resources in a seamless and controlled manner. It manages memory, allocates system resources, handles input/output operations, and facilitates communication between software and hardware.

In simpler terms, you can think of the kernel as a traffic controller or a manager. It ensures that different software applications can safely and efficiently use the computer's resources without interfering with each other. It also protects the system from unauthorized access and handles error handling and recovery.

While the kernel itself is not directly visible to the end-users, its smooth functioning is crucial for the overall operation and performance of the operating system. It plays a vital role in providing a stable and secure environment for applications to run on your computer.

In summary, the kernel is the core component of an operating system that acts as a bridge between software and hardware, managing resources and enabling smooth communication between them.

🔹What is Shell

The shell is a program that provides a command-line interface to interact with an operating system. It acts as a mediator between the user and the underlying operating system, allowing users to execute commands and run programs.

🔹What is Linux Shell Scripting?

Linux shell scripting refers to the practice of writing scripts or programs using shell commands and constructs to automate tasks and perform various operations in a Linux environment. It involves writing a series of commands in a script file, which can then be executed to carry out a specific set of actions.

Shell scripting allows users to combine and sequence multiple commands, control the flow of execution, and handle input/output operations, making it a powerful tool for automating repetitive tasks, system administration, and creating customized workflows.

Shell scripts are typically written in scripting languages like Bash (Bourne Again Shell), which is the default shell on most Linux systems. However, other shell languages like Zsh, Ksh, and Csh are also used in different Linux distributions.

Some common use cases for Linux shell scripting include:

  1. Task Automation: Shell scripts can automate routine tasks, such as file management, backups, system maintenance, and software installations.

  2. System Administration: Shell scripts are valuable for system administrators to perform administrative tasks like user management, monitoring system resources, configuring network settings, and managing services.

  3. Customization: Shell scripts enable users to customize their Linux environment by creating aliases, setting environment variables, defining personalized prompts, and creating shortcuts for complex commands.

  4. Report Generation: Shell scripts can generate reports by extracting and processing data from various sources, such as log files, databases, or command outputs.

By leveraging shell scripting, users can save time and effort, ensure consistent and repeatable actions, and streamline administrative tasks in a Linux environment.

🔹What is the role of shell scripting in DevOps?

Shell scripting plays a crucial role in DevOps by enabling automation, streamlining processes, and facilitating efficient management of infrastructure and software deployments. Here are some key roles of shell scripting in DevOps:

  1. Automation: Shell scripting allows for automating repetitive tasks, such as build and deployment processes, configuration management, and monitoring. By writing scripts, DevOps professionals can save time and effort by automating routine operations.

  2. Infrastructure as Code: Shell scripting is instrumental in implementing Infrastructure as Code (IaC) practices. Tools like Ansible, Puppet, or Chef utilize shell scripting to define and manage infrastructure resources, making it easier to provision, configure, and manage infrastructure in a repeatable and consistent manner.

  3. Continuous Integration and Continuous Deployment (CI/CD): Shell scripting is utilized in CI/CD pipelines to automate the build, testing, and deployment processes. It helps trigger different stages of the pipeline, execute tests, manage dependencies, and deploy applications to various environments seamlessly.

  4. Configuration Management: Shell scripting is employed for configuration management tasks, allowing DevOps teams to automate the setup and configuration of systems. Scripts can be used to install and configure software packages, manage system settings, and maintain consistency across different servers.

  5. Task Orchestration: Shell scripting facilitates the orchestration of complex tasks and workflows. By combining multiple commands and scripts, DevOps professionals can create sophisticated workflows that coordinate different actions and ensure proper sequencing of tasks.

Overall, shell scripting empowers DevOps practitioners to automate processes, achieve consistency, improve efficiency, and enhance collaboration across the software development lifecycle and infrastructure management. It enables organizations to deliver software more rapidly, reliably, and with greater quality.

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

The line #!/bin/bash is known as a shebang or hashbang line, and it is used in shell scripts to specify the interpreter or shell to be used to execute the script. In this case, #!/bin/bash indicates that the script should be executed using the Bash shell.

Bash (Bourne Again Shell) is a popular and widely-used shell on Linux and Unix systems. It provides advanced features and capabilities beyond what is available in the standard Bourne shell (/bin/sh).

However, it is also possible to use #!/bin/sh in the shebang line. In many systems, /bin/sh is a symbolic link or a compatibility link to the default shell, which may be Bash or another shell like Dash or Ash. Using #!/bin/sh allows the script to be executed using the system's default shell.

The choice of #!/bin/bash or #!/bin/sh in the shebang line depends on the specific requirements of the script and the desired shell features. If the script utilizes specific Bash features, it is advisable to use #!/bin/bash. On the other hand, if the script only requires basic shell functionality and needs to be compatible with different shells, using #!/bin/sh can ensure portability.

Here's an example of a simple shell script file:

  1. Open a text editor and create a new file. Let's name it first_script.sh.

  2. Add the following lines to the file:

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

    Save the file.

    In your terminal, navigate to the directory where you saved first_script.sh.

    Make the script file executable by running the following command:

     chmod +x first_script.sh
    
     ./first_script.sh
    
     I will complete #90DaysOfDevOps challenge
    

    Here's an example of a shell script that takes user input and command-line arguments, and then prints the variables:

     #!/bin/bash
    
     # Taking user input
     read -p "Enter your name: " name
    
     # Printing variables
     echo "User input: $name"
    

    Let's assume you save this script in a file named input_script.sh. To use this script:

    Open a terminal and navigate to the directory where input_script.sh is saved.

    Make the script file executable by running the following command:

     chmod +x input_script.sh
    

    Execute the script with or without a command-line argument:

     ./input_script.sh
    

if-else statements: -

Here's an example of using if-else statements in a shell script to compare two numbers:

#!/bin/bash

# Assigning values to variables
num1=10
num2=20

# Comparing the numbers
if [ $num1 -eq $num2 ]; then
    echo "The numbers are equal."
elif [ $num1 -gt $num2 ]; then
    echo "Number 1 is greater than Number 2."
else
    echo "Number 2 is greater than Number 1."
fi

In this example, we compare the values of num1 and num2 using if-else statements. Here's what the script does:

  1. Assign values to the variables num1 and num2.

  2. Use the if statement to check if num1 is equal to num2 using the -eq operator.

  3. If the condition is true, execute the corresponding code block and print "The numbers are equal."

  4. If the condition in the if statement is false, use the elif statement to check if num1 is greater than num2 using the -gt operator.

  5. If the condition in the elif statement is true, execute the corresponding code block and print "Number 1 is greater than Number 2."

  6. If both the conditions in the if and elif statements are false, execute the else block and print "Number 2 is greater than Number 1."

You can modify the values of num1 and num2 to compare different numbers. The script will output the appropriate message based on the comparison.

chmod +x ifelse_script.sh

Here's an example of using if-else statements in a shell script to compare two numbers:

./ifelse_script.sh

Here's an example of using if-else statements in a shell script to compare two numbers:

for loop: -A for loop is used to iterate over a sequence of values or elements. It allows you to execute a set of commands repeatedly, with each iteration performing a specific action.

Here's an example of a for loop in shell scripting:

#!/bin/bash

# Example 1: Loop through a range of numbers
echo "Example 1: Loop through a range of numbers"
for i in {1..5}
do
    echo "Number: $i"
done
echo

# Example 2: Loop through elements of an array
echo "Example 2: Loop through elements of an array"
fruits=("Apple" "Banana" "Orange" "Mango")
for fruit in "${fruits[@]}"
do
    echo "Fruit: $fruit"
done
echo

# Example 3: Loop through files in a directory
echo "Example 3: Loop through files in a directory"
for file in *
do
    echo "File: $file"
done

In this example, we have three different examples of for loops:

  1. Loop through a range of numbers: The loop iterates through the numbers 1 to 5 using the {1..5} range expression.

  2. Loop through elements of an array: The loop iterates through each element of the fruits array and prints its value.

  3. Loop through files in a directory: The loop iterates through all files in the current directory using the * wildcard, and prints their names.

chmod +x for_loop.sh
./for_loop.sh

When you run the script, it will output:

Example 1: Loop through a range of numbers
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Example 2: Loop through elements of an array
Fruit: Apple
Fruit: Banana
Fruit: Orange
Fruit: Mango

Example 3: Loop through files in a directory
File: file1.txt
File: script.sh
File: directory

while loop: - while loop is used to repeatedly execute a set of commands as long as a specified condition is true. The loop continues until the condition becomes false.

Here's an example of a while loop in shell scripting:

#!/bin/bash

read -p "enter the number:" num
count=0

while [ $count -le $num ]
do
    echo $count
    let count++
done
chmod +x while_loop.sh
./while_loop.sh

switch case: -The switch-case construct allows you to perform different actions based on the value of a variable or expression. Although switch-case is not natively supported in standard shell scripting, it can be achieved using the case statement.

Here's an example of a while loop in shell scripting:

#!/bin/bash

echo "Select one of the following choices:"
echo "1. Show all files"
echo "2. Show present working directory"
echo "3. View date"

read choices

case $choices in
    1)
        ls
        ;;
    2)
        pwd
        ;;
    3)
        date
        ;;
    *)
        echo "Invalid choice. Please try again."
        ;;
esac
chmod +x switch_case.sh
./switch_case.sh
  • The script starts by displaying a menu of choices to the user using the echo command.

  • The read command is used to capture the user's input and store it in the variable choices.

  • The case statement checks the value of $choices and executes the corresponding code block based on the selected choice.

    • If the user enters 1, the script executes the ls command, which lists all files in the current directory.

    • If the user enters 2, the script executes the pwd command, which displays the present working directory.

    • If the user enters 3, the script executes the date command, which displays the current date and time.

    • If the user enters any other value, the script executes the default code block, which displays the message "Invalid choice. Please try again."

The purpose of this script is to provide a menu-based interface for the user to select different actions. It demonstrates the use of a case statement to handle different choices and execute the appropriate commands based on the user's input.

After running the script, the user will be prompted to enter their choice. Based on the selected choice, the script will perform the corresponding action and display the output.

arguments: - In shell scripting, arguments allow you to pass values to a script at the time of execution. These arguments provide a way to customize the behavior of the script based on the input provided by the user or other programs.

Here's an example script to demonstrate the usage of arguments:

#!/bin/bash

a=$1
b=$2
echo "it is first argument is $a"
echo "second arguments is $b"
chmod +x argument_script.sh
./argument_script.sh Hello World

Cron: Cron is a time-based job scheduler in Unix-like operating systems. It is a daemon (cronie, cron, or crond) that runs in the background and executes scheduled commands or scripts automatically. Cron is responsible for the actual execution of scheduled tasks at the specified times or intervals.

Crontab: Crontab (short for "cron table") is a file that contains a list of scheduled commands or scripts to be executed by the cron daemon. Each user on a Unix-like system can have their own crontab file. The crontab file defines the schedule for the user's cron jobs.

The crontab file is edited using the crontab command. It allows users to manage their scheduled tasks by adding, modifying, or removing cron job entries. Each line in the crontab file represents a separate cron job, specifying the schedule and the command or script to be executed.

In summary, cron is the background daemon responsible for executing scheduled tasks, while crontab is a file that allows users to define and manage their scheduled commands or scripts for cron to execute.

Here's a brief comparison between cron and crontab:

  • Cron:

    • Daemon responsible for executing scheduled tasks.

    • Runs in the background continuously.

    • Executes commands or scripts based on the schedule defined in crontab files.

    • Managed by the system administrator.

  • Crontab:

    • File containing a user's scheduled commands or scripts.

    • Edited using the crontab command.

    • Defines the schedule and commands for individual cron jobs.

    • Managed by the individual user.

Understanding the difference between cron and crontab helps in effectively scheduling and managing tasks on Unix-like systems, allowing for automated execution of commands or scripts at specific times or intervals.

Here's an example to help you understand how to set up a cron job using the crontab command for the first time:

  1. Open a terminal or shell session.

  2. Type the following command to open the crontab file for editing:

     crontab -e
    
  1. If it's your first time setting up a cron job, you may be prompted to choose an editor. Select your preferred editor (e.g., nano, vim, or emacs).

  2. Once the editor opens, you can add your cron job entry. Each line in the crontab file represents a separate cron job. The structure of a cron job line is as follows:

* * * * * command

The five * represent the minute, hour, day of the month, month, and day of the week, respectively. Each field can accept specific values or wildcard characters to indicate all possible values. The command represents the command or script you want to schedule.

In this case, we will set the minute field to 22 and the hour field to 23 to represent 11:22 PM.

22 23 * * * echo "hi guys" > /home/gopal/Pictures/test_crontab.txt