Linux - day 4

Linux - day 4

Basic Linux Shell Scripting for DevOps Engineers

As a DevOps engineer, learning Linux shell scripting is key to making your work more efficient. It allows you to automate tasks, handle system operations, and make it easier for users to interact with the operating system. Let's break down the basics of Linux, shell scripting, and go over some tasks that will get you started.


What is a Kernel?

Think of the kernel as the brain of your computer. It's the core part of the operating system that manages everything—memory, CPU, input/output devices, and more. The kernel makes sure all parts of your system are working together smoothly.


What is a Shell?

The shell is like a translator between you (the user) and the kernel (the system's brain). It takes the commands you type and translates them into instructions that the system can understand and execute. When you open a terminal in Linux, you’re working with the shell to interact with the system directly by typing commands. It’s your main tool to control the system.


What is Linux Shell Scripting?

Linux shell scripting is the process of writing a series of commands (a script) that the shell can execute. These scripts help automate repetitive tasks, handle system administration, and make managing systems easier and more efficient. Shell scripting is super important in DevOps because it allows you to automate deployments, monitor systems, manage backups, and so much more.


Tasks

1. Shell Scripting for DevOps: In Simple Terms

In DevOps, shell scripting is all about automation. As a DevOps engineer, you’ll often deal with multiple servers, services, and environments. Manually performing tasks on each one can be time-consuming and prone to errors. Shell scripts allow you to automate these tasks—saving you time and ensuring everything runs consistently.

For example, instead of manually updating software on 10 servers, you can write a shell script to do it automatically. Other common tasks you can automate include deploying code, monitoring system performance, and backing up data.


2. What is #!/bin/bash? Can We Write #!/bin/sh Instead?

#!/bin/bash is the shebang line at the start of a shell script. It tells the system which interpreter (shell) to use to run the script. In this case, it’s using the Bash shell (Bourne Again Shell), which is one of the most common shells in Linux.

Yes, you can write #!/bin/sh instead. The difference is that sh refers to the Bourne shell, which is simpler and has fewer features than bash. While most Linux systems have sh linked to bash, for advanced scripting it’s better to stick with bash.


3. Shell Script to Print "I will complete #90DaysOfDevOps challenge"

Here’s a basic shell script that prints out the message:

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

To run this:

  1. Save the script in a file called challenge.sh.

  2. Give it permission to execute by running chmod +x challenge.sh.

  3. Run the script with ./challenge.sh.


4. Shell Script to Take User Input and Print Variables

This script takes input directly from the user and from arguments passed in when you run the script:

#!/bin/bash

# Take user input
echo "What's your name?"
read name

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

# Print out the results
echo "Hello, $name!"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"

To run this script:

  1. Save it as input_script.sh.

  2. Make it executable (chmod +x input_script.sh).

  3. Run the script by passing arguments like this:

     ./input_script.sh DevOpsEngineer Coding
    

It will greet the user by name and print out the two arguments you passed in.


5. Example of an If-Else Statement in Shell Scripting

Here’s an example script that compares two numbers and prints out which one is greater:

#!/bin/bash

# Get numbers from the user
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read 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 "Both numbers are equal"
fi

This script will prompt the user for two numbers, then compare them and let you know which one is bigger, smaller, or if they are equal.


Wrapping Up

Shell scripting is a powerful tool for DevOps engineers, allowing you to automate tasks and make your workflows more efficient. Whether it’s deploying code, backing up files, or monitoring systems, writing and executing shell scripts will save you time and make your operations run smoothly.