149 Essential Shell Script Examples for Linux Sysadmins
This article introduces a free 68‑page collection of 149 practical Bash scripts covering common Linux tasks such as finding zombie processes, cleaning empty files, calculating statistics, formatting dates, checking file existence, sorting numbers, retrieving MAC addresses, and determining leap years, all with ready‑to‑copy code.
In most Linux, Unix, and Unix‑like systems, the shell is the primary way users interact with the operating system kernel. Whether you are an operations engineer or a developer, mastering shell commands is an essential skill.
The "Shell Script 149 Cases" ebook provides 68 pages of clear, searchable, and copy‑ready Bash scripts that help improve practical abilities. Below are selected examples from the collection.
1. Find zombie processes
#!/bin/bash
# Find zombie processes in Linux
# awk checks the 8th column of ps output for "Z" and prints PID and command
ps aux | awk '{if($8 == "Z"){print $2,$11}}'2. Delete zero‑size files in a directory
#!/bin/bash
# Delete zero‑size files in a directory
# /var/www/html is the test directory; the script removes all 0‑byte files
dir="/var/www/html"
find $dir -type f -size 0 -exec rm -rf {} \;3. Sum, min, and max of five numbers under 100
#!/bin/bash
COUNT=1
SUM=0
MIN=0
MAX=100
while [ $COUNT -le 5 ]; do
read -p "Please enter an integer (1‑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 "Weekday abbreviation (e.g., Sun)"
date +%a
echo "Weekday full name (e.g., Sunday)"
date +%A
echo "Month abbreviation (e.g., Jan)"
date +%b
echo "Month full name (e.g., January)"
date +%B
echo "Numeric month (e.g., 12)"
date +%m
echo "Numeric day (e.g., 01)"
date +%d
echo "Year (e.g., 2023)"
date +%Y
echo "YYYY‑MM‑DD"
date +%F
echo "Hour (24‑hour)"
date +%H
echo "Minute (00‑59)"
date +%M
echo "Second"
date +%S
echo "Nanosecond"
date +%N
echo "Combined format"
date +"%Y%m%d %H:%M:%S"5. Check if a file or directory exists
#!/bin/bash
# Check if a file or directory exists
if [ $# -eq 0 ]; then
echo "No arguments provided, please input a parameter"
echo "Usage: $0 [filename|dirname]"
fi
if [ -f $1 ]; then
echo "The file exists"
ls -l $1
else
echo "File not found"
fi
if [ -d $1 ]; then
echo "The directory exists"
ls -ld $1
else
echo "Directory not found"
fi6. Sort three input integers
#!/bin/bash
# Prompt user to input 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
if [ $num2 -gt $num3 ]; then
tmp=$num2; num2=$num3; num3=$tmp
fi
echo "Sorted numbers (small to large): $num1, $num2, $num3"7. Get the local MAC address
#!/bin/bash
# Retrieve the local MAC address
ip a s | awk 'BEGIN{print "Local MAC address info:"}/^[0-9]/{print $2;getline;if($0~/link\/ether/){print $2}}' | grep -v lo:8. Determine if a 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 ]; then
echo "$year is a leap year"
elif [ $((year % 400)) -eq 0 ]; then
echo "$year is a leap year"
else
echo "$year is not a leap year"
fiThe full ebook contains many more scripts, all organized with clear directories and searchable titles, allowing readers to quickly locate and copy useful Bash snippets for everyday Linux administration tasks.
Ops Community
A leading IT operations community where professionals share and grow together.
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.
