Operations 13 min read

10 Must‑Know Shell Scripts to Boost Your Ops Efficiency

This guide presents ten practical shell script examples for operations engineers, covering file consistency checks, colored output functions, FTP downloads, package verification, service status monitoring, host reachability, resource utilization alerts, batch disk usage monitoring, website availability testing, and MySQL master‑slave synchronization, all with full code snippets.

Efficient Ops
Efficient Ops
Efficient Ops
10 Must‑Know Shell Scripts to Boost Your Ops Efficiency

In daily operations, shell scripts are essential tools. With the rapid development of AI large‑model coding, script generation has improved, but experienced ops engineers must still review scripts for safety, stability, and compatibility. The following ten shell script cases help you automate common tasks and increase operational efficiency.

1. Check file consistency between two servers

<code>#!/bin/bash
######################################
# Detect file consistency between two servers
######################################

dir=/data/web
b_ip=192.168.88.10
# Generate MD5 list on the local machine
find $dir -type f | xargs md5sum > /tmp/md5_a.txt
# Generate MD5 list on the remote machine
ssh $b_ip "find $dir -type f | xargs md5sum > /tmp/md5_b.txt"
# Retrieve the remote list
scp $b_ip:/tmp/md5_b.txt /tmp
# Compare the two lists
for f in $(awk '{print $2}' /tmp/md5_a.txt); do
    if grep -qw "$f" /tmp/md5_b.txt; then
        md5_a=$(grep -w "$f" /tmp/md5_a.txt | awk '{print $1}')
        md5_b=$(grep -w "$f" /tmp/md5_b.txt | awk '{print $1}')
        if [ "$md5_a" != "$md5_b" ]; then
            echo "$f changed."
        fi
    else
        echo "$f deleted."
    fi
done
</code>

2. Define a colored output function

<code># Method 1
function echo_color() {
    if [ "$1" == "green" ]; then
        echo -e "[32;40m$2[0m"
    elif [ "$1" == "red" ]; then
        echo -e "[31;40m$2[0m"
    fi
}

# Method 2 (case statement)
function echo_color() {
    case $1 in
        green) echo -e "[32;40m$2[0m" ;;
        red)   echo -e "[31;40m$2[0m" ;;
        *)     echo "Example: echo_color red string" ;;
    esac
}

# Usage example
# echo_color green "test"
</code>

3. Download a file from an FTP server

<code>#!/bin/bash
if [ $# -ne 1 ]; then
    echo "Usage: $0 filename"
    exit 1
fi

dir=$(dirname $1)
file=$(basename $1)
ftp -n -v <<EOF
open 192.168.1.10
user admin password
binary
cd $dir
get "$file"
EOF
</code>

4. Check if a software package is installed

<code>#!/bin/bash
if rpm -q sysstat &>/dev/null; then
    echo "sysstat is already installed."
else
    echo "sysstat is not installed!"
fi
</code>

5. Check service status and send alert

<code>#!/bin/bash
PORT_C=$(ss -anu | grep -c 123)
PS_C=$(ps -ef | grep ntpd | grep -vc grep)
if [ $PORT_C -eq 0 -o $PS_C -eq 0 ]; then
    echo "Content" | mail -s "Subject" [email protected]
fi
</code>

6. Verify host reachability

Method 1 – three‑attempt array check:

<code>#!/bin/bash
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in $IP_LIST; do
    NUM=1
    while [ $NUM -le 3 ]; do
        if ping -c 1 $IP > /dev/null; then
            echo "$IP Ping is successful."
            break
        else
            FAIL_COUNT[$NUM]=$IP
            let NUM++
        fi
    done
    if [ ${#FAIL_COUNT[*]} -eq 3 ]; then
        echo "${FAIL_COUNT[1]} Ping is failure!"
        unset FAIL_COUNT[*]
    fi
done
</code>

Method 2 – counter variable:

<code>#!/bin/bash
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in $IP_LIST; do
    FAIL_COUNT=0
    for ((i=1;i<=3;i++)); do
        if ping -c 1 $IP > /dev/null; then
            echo "$IP Ping is successful."
            break
        else
            let FAIL_COUNT++
        fi
    done
    if [ $FAIL_COUNT -eq 3 ]; then
        echo "$IP Ping is failure!"
    fi
done
</code>

7. Monitor CPU, memory, and disk utilization

CPU monitoring (using

vmstat

):

<code>#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=$(ifconfig eth0 | awk -F '[ :]+' '/inet addr/{print $4}')
MAIL="[email protected]"
if ! which vmstat &>/dev/null; then
    echo "vmstat command not found, please install procps package."
    exit 1
fi
US=$(vmstat | awk 'NR==3{print $13}')
SY=$(vmstat | awk 'NR==3{print $14}')
USE=$((US+SY))
if [ $USE -ge 50 ]; then
    echo -e "Date: $DATE\nHost: $IP\nProblem: CPU utilization $USE" | mail -s "CPU Monitor" $MAIL
fi
</code>

Memory monitoring (using

free

):

<code>#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=$(ifconfig eth0 | awk -F '[ :]+' '/inet addr/{print $4}')
MAIL="[email protected]"
TOTAL=$(free -m | awk '/Mem/{print $2}')
USE=$(free -m | awk '/Mem/{print $3-$6-$7}')
FREE=$((TOTAL-USE))
if [ $FREE -lt 1024 ]; then
    echo -e "Date: $DATE\nHost: $IP\nProblem: Total=$TOTAL, Use=$USE, Free=$FREE" | mail -s "Memory Monitor" $MAIL
fi
</code>

Disk monitoring (using

fdisk

and

df

):

<code>#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=$(ifconfig eth0 | awk -F '[ :]+' '/inet addr/{print $4}')
MAIL="[email protected]"
TOTAL=$(fdisk -l | awk -F '[: ]+' 'BEGIN{OFS="="}/^Disk /dev/{printf "%s=%sG,",$2,$3}')
PART_USE=$(df -h | awk 'BEGIN{OFS="="}/^\/dev/{print $1,int($5),$6}')
for i in $PART_USE; do
    PART=$(echo $i | cut -d"=" -f1)
    USE=$(echo $i | cut -d"=" -f2)
    MOUNT=$(echo $i | cut -d"=" -f3)
    if [ $USE -gt 80 ]; then
        echo -e "Date: $DATE\nHost: $IP\nTotal: $TOTAL\nProblem: $PART=$USE($MOUNT)" | mail -s "Disk Monitor" $MAIL
    fi
done
</code>

8. Batch host disk usage monitoring (SSH)

<code>#!/bin/bash
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
    USER=$(awk -v ip=$IP '$1==ip{print $2}' $HOST_INFO)
    PORT=$(awk -v ip=$IP '$1==ip{print $3}' $HOST_INFO)
    TMP_FILE=/tmp/disk.tmp
    ssh -p $PORT $USER@$IP df -h > $TMP_FILE
    USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $1,int($5)}' $TMP_FILE)
    for USE_RATE in $USE_RATE_LIST; do
        PART_NAME=${USE_RATE%=*}
        RATE=${USE_RATE#*=}
        if [ $RATE -ge 80 ]; then
            echo "Warning: $PART_NAME Partition usage $RATE%!"
        fi
    done
done
</code>

9. Check website availability

Method 1 – using

curl

:

<code>check_url() {
    HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $1)
    if [ $HTTP_CODE -ne 200 ]; then
        echo "Warning: $1 Access failure!"
    fi
}
# Usage: check_url www.baidu.com
</code>

Method 2 – using

wget

:

<code>check_url() {
    if ! wget -T 10 --tries=1 --spider $1 >/dev/null 2>&1; then
        echo "Warning: $1 Access failure!"
    fi
}
</code>

Multiple attempts (three‑times check) can be implemented with loops similar to the host‑reachability scripts above.

10. Verify MySQL master‑slave synchronization status

<code>#!/bin/bash
USER=bak
PASSWD=123456
IO_SQL_STATUS=$(mysql -u$USER -p$PASSWD -e "show slave status" | awk -F: '/Slave_.*_Running/{gsub(": ",":");print $0}')
for i in $IO_SQL_STATUS; do
    THREAD_STATUS_NAME=${i%:*}
    THREAD_STATUS=${i#*:}
    if [ "$THREAD_STATUS" != "Yes" ]; then
        echo "Error: MySQL Master‑Slave $THREAD_STATUS_NAME status is $THREAD_STATUS!"
    fi
done
</code>

These scripts provide a quick reference for common operational tasks, enabling faster troubleshooting and proactive monitoring.

monitoringautomationoperationsLinuxshell
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login 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.