149 Practical Shell Script Examples Every Ops Engineer Should Know
This article presents a curated selection of 149 shell script cases—including tasks such as locating zombie processes, removing empty files, summing numbers, formatting dates, checking file existence, sorting integers, retrieving MAC addresses, and determining leap years—to help operations engineers automate routine work and boost productivity.
For operations engineers, shell scripts can reduce repetitive work and greatly improve efficiency; mastering them is an essential skill.
Below are selected examples from the "149 Practical Shell Script Cases" collection (the full set contains 68 pages with clear indexing). Each example includes a brief description and the corresponding Bash code.
1. Find zombie processes in Linux
#!/bin/bash
# Find zombie processes
ps aux | awk '{if($8 == "Z"){print $2,$11}}'2. Delete zero‑size files in a directory
#!/bin/bash
# Delete zero‑size files
dir="/var/www/html"
find $dir -type f -size 0 -exec rm -rf {} \;3. Read five numbers (0‑100) and compute sum, min, and max
#!/bin/bash
COUNT=1
SUM=0
MIN=0
MAX=100
while [ $COUNT -le 5 ]; do
read -p "Please enter an integer (0‑100): " INT
if [[ ! $INT =~ ^[0-9]+$ ]]; then
echo "Input must be an integer!"
exit 1
elif [ $INT -gt 100 ]; then
echo "Input must be within 100!"
exit 1
fi
SUM=$((SUM+INT))
[ $MIN -lt $INT ] && MIN=$INT
[ $MAX -gt $INT ] && MAX=$INT
let COUNT++
done
echo "SUM: $SUM"
echo "MIN: $MIN"
echo "MAX: $MAX"4. Print various time formats
#!/bin/bash
# Print various time formats
echo "Display abbreviated weekday (e.g., Sun)"
date +%a
echo "Display full weekday (e.g., Sunday)"
date +%A
echo "Display abbreviated month (e.g., Jan)"
date +%b
echo "Display full month (e.g., January)"
date +%B
echo "Display numeric month (e.g., 12)"
date +%m
echo "Display numeric day (e.g., 01)"
date +%d
echo "Display numeric year (e.g., 2023)"
date +%Y
echo "Display year‑month‑day"
date +%F
echo "Display hour (24‑hour)"
date +%H
echo "Display minute (00..59)"
date +%M
echo "Display second"
date +%S
echo "Display nanosecond"
date +%N
echo "Combined format"
date +"%Y%m%d %H:%M:%S"5. Check whether a file or directory exists
#!/bin/bash
# Check existence of file or directory
if [ $# -eq 0 ]; then
echo "No arguments provided, please specify a file or directory"
echo "Usage: $0 [filename|dirname]"
exit 1
fi
if [ -f "$1" ]; then
echo "The file exists"
ls -l "$1"
elif [ -d "$1" ]; then
echo "The directory exists"
ls -ld "$1"
else
echo "The specified file or directory does not exist"
fi6. Sort three input integers
#!/bin/bash
# Prompt user for three integers and sort them
read -p "Please enter an integer: " num1
read -p "Please enter an integer: " num2
read -p "Please enter an integer: " num3
# Ensure num1 holds the smallest value
if [ $num1 -gt $num2 ]; then
tmp=$num1; num1=$num2; num2=$tmp
fi
if [ $num1 -gt $num3 ]; then
tmp=$num1; num1=$num3; num3=$tmp
fi
# Ensure num2 holds the middle value
if [ $num2 -gt $num3 ]; then
tmp=$num2; num2=$num3; num3=$tmp
fi
echo "Sorted numbers (ascending): $num1, $num2, $num3"7. Retrieve the local MAC address
#!/bin/bash
# Retrieve local MAC address
ip a s | awk 'BEGIN{print "Local MAC address information:"}/^[0-9]/{print $2;getline;if($0~/link\/ether/){print $2}}' | grep -v lo:8. Determine whether a given year is a leap year
#!/bin/bash
# Prompt for a year and determine if it is a leap year
read -p "Please enter a year: " year
if [ -z "$year" ]; then
echo "No year entered"
exit 1
fi
if [[ "$year" =~ [a-zA-Z] ]]; then
echo "Input is not a number"
exit 1
fi
if [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ] || [ $((year % 400)) -eq 0 ]; then
echo "$year is a leap year"
else
echo "$year is not a leap year"
fiSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
