Day 3:Shell Scripting

Aditya TiwaryAditya Tiwary
3 min read

🚦 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 with pipefail, 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

🔗 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

bashCopyEditcurl URL > image.png

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 peppering sudo 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

ConceptRole in DevOps Scripting
set -euo pipefailEnsures safe and clear execution
ps, grep, findProvide insight and filtering on processes/files
curl, wgetAutomate network fetches, logs, and artifacts
sudo, suManage permissions securely
Loops and conditionalsEnable logical and repetitive automation patterns

These commands are the backbone of many DevOps scripts—used in monitoring, automation, maintenance, and deployment pipelines.

0
Subscribe to my newsletter

Read articles from Aditya Tiwary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Tiwary
Aditya Tiwary