Day 1 || Comprehensive Guide to Shell Scripting in DevOps

Day 1 || Comprehensive Guide to Shell Scripting in DevOps

Introduction:

echo $SHELL
bash --version
cat /etc/shells

Shell scripting is a fundamental skill for DevOps professionals. In this article, we will explore the basics of shell scripting, understand why it's crucial in DevOps, and dive into practical examples using common Linux commands like df, free, nproc, ps, grep, and awk. Additionally, we'll learn how to use the set -x option for debugging purposes.

What is Shell Scripting

Shell scripting as the process of writing and executing scripts (sequences of commands) in a Unix-like shell (e.g., Bash).

Importance in DevOps

scripting is essential in the DevOps context, enabling automation, consistency, and efficient system administration.

Common Linux Commands

In this section, we'll briefly introduce each of the following commands and their purposes:

  • df -h: Explain how df displays disk usage information, and -h makes the output human-readable.

  • free: Describe how the free command provides system memory (RAM) usage information.

  • nproc: Discuss how nproc shows the number of CPU cores.

  • ps -ef: Explain how ps it is used to list processes, and the -ef options provide a detailed view of all processes.

  • grep: Describe how grep it is used for searching and filtering text data.

  • awk: Introduce awk as a text-processing tool for data extraction and manipulation.

Creating a Shell Script

  • Choosing a Text Editor: Suggest a text editor like Nano or Vim for writing scripts.

  • Creating Your First Script File: Show how to create a new file with a .sh extension, e.g., system_info.sh

to retrieve and display system information. Here's the script:

#!/bin/bash

# A DevOps System Information Script

# Display disk usage information
echo "Disk Usage Information:"
df -h

# Display memory (RAM) usage information
echo "Memory Usage Information:"
free

# Display the number of CPU cores
echo "Number of CPU Cores:"
nproc

# Display a list of running processes
echo "List of Running Processes:"
ps -ef

# Search for the "sshd" process using grep
echo "Search for the 'sshd' Process:"
ps -ef | grep sshd

# Use awk to print the user and process ID of the "sshd" process
echo "User and Process ID of 'sshd' Process:"
ps -ef | grep sshd | awk '{print $1, $2}'

Explanation of the Script:

  • This script begins with the shebang line (#!/bin/bash) to specify that it should be interpreted using the Bash shell.

  • It includes comments to provide explanations for each section of the script.

  • It combines the df -h, free, nproc, ps -ef, grep, and awk commands to collect and display various system information.

  • For example, it displays disk usage, memory usage, the number of CPU cores, a list of running processes, searches for the "sshd" process using, and extracts specific information about the "sshd" process using awk.

You can save this script to a file (e.g., system_info.sh) and make it executable using chmod +x system_info.sh. When you run it (./system_info.sh), it will display the system information as described in the script.

Day 1 Lab Task: Your First Shell Script

Task Description:

In this Day 1 lab task, you will create your very first shell script. You'll write a script that displays a personalized greeting message when executed.

Task Steps:

Step 1: Open a Text Editor

Open your preferred text editor on your Linux system. Common text editors include nano, vim, and gedit. Choose the one you are most comfortable with.

Step 2: Create a New File

Create a new file with a .sh extension, e.g., greeting.sh, in your chosen text editor. This file will contain your shell script.

Step 3: Write Your Script

In the newly created file, write the following lines of code to create a simple shell script:

#!/bin/bash

# This is your first shell script
# It will greet you with a message

echo "Hello, DevOps Apprentice! Welcome to your first shell script."
  • The first line (#!/bin/bash) is the shebang line, specifying that this script should be interpreted using the Bash shell.

  • The subsequent lines are comments (lines starting with #) that provide explanations about the script.

  • The echo command is used to print the greeting message to the terminal.

Step 4: Save the Script

Save the file with your script. If you are using nano, press Ctrl+O to save and Ctrl+X to exit.

Step 5: Make the Script Executable

In your terminal, navigate to the directory where you saved the script. Use the chmod +x greeting.sh command to make the script executable.

Step 6: Run Your Script

Execute your script by running ./greeting.sh in the terminal. You should see the greeting message displayed on the screen.

Step 7: Experiment and Modify

Feel free to modify your script. Change the greeting message or add more echo statements to print additional messages. Experiment with different messages and see how they are displayed when you run the script.

Did you find this article valuable?

Support Aqib Hafeez(DevOps enthusiast) by becoming a sponsor. Any amount is appreciated!