Day 3:Shell Scripting


🚦 1. Analyzing Node Health
A core DevOps script often checks CPU, memory, disk usage, and running processes to ensure “node health.” A sample snippet:
bashCopyEdit#!/bin/bash
set -x
set -e
set -o pipefail
# Check disk space
df -h | grep '/$'
# Check node processes
ps -ef | grep my_app
set -x
echoes each command before execution—great for transparency in logs DEV CommunityStack Overflow.set -e
halts on any failed command; combined withpipefail
, even a failure in a pipeline is caught How-To GeekGitHub.
🔍 2. set -x
, -e
, -o pipefail
set -x
: Enables debug mode, revealing commands and arguments via stderr before execution—perfect for tracing PullRequest+1GitHub+1.set -e
: Abort script when any command exits with a non-zero status (error) Stack Overflow+4PullRequest+4GitHub+4.set -o pipefail
: Ensures the script dies if any stage in a pipeline fails, not just the final one—critical for robust workflows GitHub.
Combined best-practice shebang:
bashCopyEdit#!/bin/bash
set -euo pipefail
⚙️ 3. Process Listing: ps -ef
and grep
bashCopyEditps -ef | grep my_service | grep -v grep
ps -ef
lists all processes in full format.Use
grep
to filter. Addgrep -v grep
or other mechanisms to avoid matching your own filter process Unix & Linux Stack Exchange+2golinuxcloud.com+2Devhints.io cheatsheets+2Stack Overflow+1DigitalOcean+1.
🔗 4. Piping: |
The pipe |
streams the output of one command into another:
bashCopyEditps -ef | grep httpd | awk '{print $2}'
Efficiently chain text tools for powerful data extraction.
🌐 5. Downloading & Fetching: curl
& wget
curl -o image.png URL
downloads a remote image to your local disk GitHub+14Unix & Linux Stack Exchange+14Hostinger+14.Alternatively:
bashCopyEditcurl URL > image.png
wget URL
does the same, with-O filename
to customize names Stack OverflowAsk Ubuntu.
Use wget -i urls.txt -P downloads/
to download multiple URLs from a list dougie.io.
🔦 6. Searching Files: find
Useful to locate files by name, type, or properties:
bashCopyEditfind /var/log -type f -name "*.log" -mtime -7
Useful in maintenance scripts to clean up or report recent logs.
🛡️ 7. Privilege Switching: sudo
and su
Prefer launching whole scripts with
sudo myscript.sh
, rather than pepperingsudo
inside the script Ask Ubuntu.For user-switching mid-script:
su user -c "command"
—but only works in password-less or root-run environments;sudo -u username cmd
is more robust Stack Overflow.
🔁 8. Control Flow: for
Loops & if-else
For-loop example:
bashCopyEditfor file in /var/log/*.log; do
echo "Analyzing $file"
grep -q "ERROR" "$file" && echo "Errors found in $file"
done
If-else example:
bashCopyEditif curl -s http://localhost/health | grep -q "OK"; then
echo "Node is healthy"
else
echo "⚠️ Node health check failed"
exit 1
fi
This combines HTTP checking, piping, grep filtering, and flow control seamlessly.
Some Commands Cheat Sheets
.
🧭 Wrapping Up
Concept | Role in DevOps Scripting |
set -euo pipefail | Ensures safe and clear execution |
ps , grep , find | Provide insight and filtering on processes/files |
curl , wget | Automate network fetches, logs, and artifacts |
sudo , su | Manage permissions securely |
Loops and conditionals | Enable logical and repetitive automation patterns |
These commands are the backbone of many DevOps scripts—used in monitoring, automation, maintenance, and deployment pipelines.
Subscribe to my newsletter
Read articles from Aditya Tiwary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
