Operations 16 min read

Essential Bash Scripts for Linux Engineers: 35 Handy Utilities

This article compiles 35 practical Bash scripts ranging from a number‑guessing game and network monitoring to automated MySQL backups and Tomcat management, providing Linux engineers with ready‑to‑use code snippets that boost productivity and simplify routine system‑administration tasks.

Efficient Ops
Efficient Ops
Efficient Ops
Essential Bash Scripts for Linux Engineers: 35 Handy Utilities

As a Linux engineer, writing good scripts can greatly improve work efficiency and free up time for other tasks. Below is a curated collection of useful Bash scripts, each accompanied by a brief description and the full source code.

1. User Guess Number Game

<code>#!/bin/bash
# Generate a random number between 1 and 100 and let the user guess it
num=$[RANDOM%100+1]
while :
do
  read -p "Computer generated a number 1‑100, you guess: " guess
  if [ $guess -eq $num ]; then
    echo "Congratulations, you guessed correctly"
    exit
  elif [ $guess -gt $num ]; then
    echo "Oops, too high"
  else
    echo "Oops, too low"
  fi
done
</code>

2. Count Remote IP Connections

<code>#!/bin/bash
# Show how many remote IPs are connected to the host (ssh, web, ftp, etc.)
netstat -atn | awk '{print $5}' | awk -F: '{print $1}' | sort -nr | uniq -c
</code>

3. Simple Hello World Function

<code>#!/bin/bash
function example {
  echo "Hello world!"
}
example
</code>

4. Print Tomcat PID

<code>#!/bin/sh
pidlist=$(ps -ef | grep apache-tomcat-7.0.75 | grep -v "grep" | awk '{print $2}')
echo "Tomcat Id list : $pidlist"
</code>

5. Rock‑Paper‑Scissors Game

<code>#!/bin/bash
game=(石头 剪刀 布)
num=$[RANDOM%3]
computer=${game[$num]}

echo "Choose your move:"
echo "1. 石头"
echo "2. 剪刀"
echo "3. 布"
read -p "Please enter 1‑3: " person
case $person in
  1) if [ $num -eq 0 ]; then echo "平局"; elif [ $num -eq 1 ]; then echo "你赢"; else echo "计算机赢"; fi;;
  2) if [ $num -eq 0 ]; then echo "计算机赢"; elif [ $num -eq 1 ]; then echo "平局"; else echo "你赢"; fi;;
  3) if [ $num -eq 0 ]; then echo "你赢"; elif [ $num -eq 1 ]; then echo "计算机赢"; else echo "平局"; fi;;
  *) echo "必须输入1‑3 的数字";;
esac
</code>

6. Multiplication Table (9×9)

<code>#!/bin/bash
for i in `seq 9`; do
  for j in `seq $i`; do
    echo -n "$j*$i=$[i*j] "
  done
  echo
done
</code>

7. Install Memcached from Source

<code>#!/bin/bash
# One‑click deployment of memcached
wget http://www.memcached.org/files/memcached-1.5.1.tar.gz
yum -y install gcc
tar -xf memcached-1.5.1.tar.gz
cd memcached-1.5.1
./configure
make
make install
</code>

8. Check If Current User Is Root and Install vsftpd

<code>#!/bin/bash
if [ $USER == "root" ]; then
  yum -y install vsftpd
else
  echo "You are not an administrator, cannot install software"
fi
</code>

9. Simple If‑Expression Example

<code>#!/bin/bash -xv
if [ $1 -eq 2 ]; then
  echo "wo ai wenmin"
elif [ $1 -eq 3 ]; then
  echo "wo ai wenxing"
elif [ $1 -eq 4 ]; then
  echo "wo de xin"
elif [ $1 -eq 5 ]; then
  echo "wo de ai"
fi
</code>

10‑35. Additional Utilities

The remaining scripts cover a wide range of system‑administration tasks, including:

Killing and restarting Tomcat processes.

Printing a chessboard pattern using ANSI colors.

Counting login‑capable accounts in /etc/passwd.

Backing up MySQL tables.

Real‑time network traffic display for a specific interface.

Scanning an entire /24 subnet to detect live hosts.

Creating user accounts with passwords via positional parameters.

Monitoring memory and disk space and sending alert emails.

One‑click LNMP (Linux‑Nginx‑MySQL‑PHP) deployment using RPM packages.

Reading command‑line arguments and simple file copy operations.

Various while/for loop examples for arithmetic sums and iteration.

Each script is presented in a ready‑to‑run form, allowing engineers to copy, adapt, and integrate them into their own automation workflows.

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