Day-6.2-devops

Mastering Shell Scripting for DevOps: Advanced Concepts and Best Practices

This blog post delves into advanced shell scripting concepts essential for DevOps, covering best practices, commands for system health checks, process management, and error handling. It emphasizes the importance of metadata, debugging techniques, and effective command usage, including grep, awk, curl, and trap commands.

Welcome back to our series on shell scripting for DevOps. In this post, we will explore advanced shell scripting concepts that are crucial for any DevOps engineer. If you haven't watched the previous video, I highly recommend doing so to build a solid foundation before diving into these advanced topics.

Recap of Previous Concepts

In our last session, we covered the basics of shell scripting, including how to create simple scripts, execute them, and debug using commands like df, free, nproc, and top. These commands help analyze the health of a node machine by providing information about disk space, memory usage, and CPU details.

Writing a Custom Node Health Script

Today, we will write a custom shell script named node_health.sh that checks the health of a virtual machine. This script will be useful for diagnosing issues when someone reports problems with their machine.

Starting with Shebang and Metadata

Every shell script should start with a shebang line to specify the interpreter. For our script, we will use:

#!/bin/bash

Next, we will include metadata information at the top of the script. This includes the author, date, purpose, and version of the script. For example:

Using Essential Commands

We will utilize the following commands in our script:

  • df -h: Displays disk space usage.

  • free -g: Shows memory usage.

  • nproc: Outputs the number of CPUs.

Enhancing Output Clarity

To improve the clarity of our script's output, we can use echo statements before each command to label the output. For example:

echo "Disk Space:" 
df -h

echo "Memory Usage:" 
free -g

echo "CPU Count:" 
nproc

This way, users can easily understand what each output represents.

Debugging with Set -x

For debugging, we can enable debug mode by adding set -x at the beginning of our script. This will print each command before it is executed, making it easier to trace errors.

set -x

Finding Running Processes

To check running processes, we will use the ps -ef command. If we want to filter for specific processes, we can pipe the output to grep. For example, to find Amazon processes:

ps -ef | grep amazon

Extracting Process IDs with awk

To extract specific information, such as process IDs, we can use the awk command. For instance, to get the process IDs of Amazon processes:

ps -ef | grep amazon | awk '{print $2}'

Error Handling with set -e and set -o pipefail

To ensure our script exits on errors, we should include set -e at the beginning. Additionally, to handle errors in piped commands, we can use set -o pipefail:

set -e
set -o pipefail

Using curl for Log Retrieval

In DevOps, checking logs is a common task. If logs are stored externally, we can use the curl command to retrieve them. For example:

curl http://example.com/logfile.log | grep error

This command fetches the log file and filters for lines containing "error".

Understanding wget

Another command to download files is wget. Unlike curl, which outputs to the terminal, wget saves the file locally:

wget http://example.com/logfile.log

Finding Files with find

To locate files on the system, we can use the find command. For example, to find a file named pam.d:

find / -name pam.d

Control Structures: if-else and for Loops

Shell scripting also allows for control structures like if-else and for loops. Here’s a simple example of an if-else statement:

if [ $a -gt $b ]; then
    echo "A is greater than B"
else
    echo "B is greater than A"
fi

And a for loop example:

for i in {1..10}; do
    echo $i
done

Trapping Signals with trap

The trap command is used to handle signals in scripts. For example, if you want to prevent a script from being interrupted by Ctrl+C, you can use:

trap "echo 'Ctrl+C is disabled'" SIGINT

Conclusion

In this post, we covered advanced shell scripting concepts that are essential for DevOps engineers. We discussed best practices for writing scripts, debugging techniques, and various commands that help manage system health and processes. Understanding these concepts will enhance your scripting skills and improve your efficiency as a DevOps professional.

Feel free to leave your questions or comments below, and I will be happy to assist you further!