Operations 8 min read

149 Practical Shell Script Examples Every Ops Engineer Should Master

Explore 149 essential shell script snippets that help operations engineers automate repetitive tasks, from finding zombie processes and cleaning empty files to sorting numbers, displaying timestamps, checking file existence, retrieving MAC addresses, and determining leap years, all with clear, copy‑ready code.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
149 Practical Shell Script Examples Every Ops Engineer Should Master

149 Practical Shell Script Examples

For operations engineers, shell scripts reduce repetitive work and significantly improve efficiency. This collection presents concise, ready‑to‑copy scripts covering common administrative tasks on Linux systems.

1. Find zombie processes

#!/bin/bash
# Find zombie processes in the Linux system
# 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 files of size 0 in a given directory (example: /var/www/html)

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 "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 date/time formats

#!/bin/bash
# Display different date 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 "Full date (YYYY‑MM‑DD)"
date +%F

echo "Hour (24‑hour)"
date +%H

echo "Minute (00‑59)"
date +%M

echo "Second"
date +%S

echo "Nanoseconds"
date +%N

echo "Combined format"
date +"%Y%m%d %H:%M:%S"

5. Check if a file or directory exists

#!/bin/bash
# Verify existence of a file or directory
if [ $# -eq 0 ]; then
  echo "No arguments provided, please specify a file or directory"
  echo "Usage: $0 [filename|directory]"
  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 "File or directory does not exist"
fi

6. Sort three input integers

#!/bin/bash
# Prompt user for three integers and sort them in ascending order
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 (small to large): $num1, $num2, $num3"

7. Retrieve the local MAC address

#!/bin/bash
# Get the MAC address of the local machine (excluding loopback)
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 given year is a leap year

#!/bin/bash
# Prompt for a year and check 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
# Leap year logic
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"
fi

These examples provide a quick reference for common shell scripting tasks, enabling engineers to copy, adapt, and integrate them into their own automation workflows.

automationoperationsLinuxsysadminbashshell scripting
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

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.