Operations 7 min read

149 Must‑Know Shell Script Examples to Boost Your Sysadmin Efficiency

This article presents a curated collection of 149 practical shell script cases, demonstrating how operations engineers can automate routine tasks such as finding zombie processes, deleting empty files, handling dates, checking file existence, sorting numbers, retrieving MAC addresses, and determining leap years, all with clear, copy‑ready Bash code.

Ops Community
Ops Community
Ops Community
149 Must‑Know Shell Script Examples to Boost Your Sysadmin Efficiency

For operations engineers, shell scripts can reduce repetitive work and greatly improve efficiency; mastering shell scripting is an essential skill.

Today we share the "149 Practical Shell Script Cases" (68 pages, clear index, searchable, copyable code) – a free resource to help you learn and practice.

Below are selected examples:

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, max

#!/bin/bash
COUNT=1
SUM=0
MIN=0
MAX=100
while [ $COUNT -le 5 ]; do
  read -p "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 ≤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 time formats
echo "Weekday abbreviation (Sun):"
date +%a
echo "Weekday full name (Sunday):"
date +%A
echo "Month abbreviation (Jan):"
date +%b
echo "Month full name (January):"
date +%B
echo "Numeric month (12):"
date +%m
echo "Numeric day (01):"
date +%d
echo "Year (2023):"
date +%Y
echo "YYYY‑MM‑DD:"
date +%F
echo "Hour (24‑h):"
date +%H
echo "Minute (00‑59):"
date +%M
echo "Second:"
date +%S
echo "Nanosecond:"
date +%N
echo "Combined:"
date +"%Y%m%d %H:%M:%S"

5. Check whether a file or directory exists

#!/bin/bash
# Check existence
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 "File exists"
  ls -l "$1"
elif [ -d "$1" ]; then
  echo "Directory exists"
  ls -ld "$1"
else
  echo "No such file or directory"
fi

6. Sort three input integers

#!/bin/bash
# Prompt for three integers and sort them
read -p "Enter first integer: " num1
read -p "Enter second integer: " num2
read -p "Enter third integer: " num3
tmp=0
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 (ascending): $num1, $num2, $num3"

7. Get local MAC address

#!/bin/bash
# Get 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 leap year

#!/bin/bash
# Leap year check
read -p "Enter a year: " year
if [ -z "$year" ]; then
  echo "No year entered"
  exit 1
fi
if [[ "$year" =~ [a-zA-Z] ]]; then
  echo "Input must be numeric"
  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"
fi
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationLinuxBashShell scriptingscript examples
Ops Community
Written by

Ops Community

A leading IT operations community where professionals share and grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.