Master Shell Scripting: From Basics to Real‑World Automation
This guide introduces shell scripting fundamentals, covering what a shell is, script creation, variables, environment variables, control structures, functions, arrays, and practical examples such as a hello‑world script, system backup, and server information collection, all illustrated with clear Bash code snippets.
Shell Scripting Introduction
The shell is a command interpreter that connects users with the operating system; a shell script is simply a sequence of Linux commands saved in a file to automate tasks and improve efficiency.
1.1 What Is Shell
Shell script overview
# Why introduce shell
# The previous article on Linux commands received many requests for a shell programming guide.
# After two weeks of preparation, this tutorial covers shell basics to practical use.
# What is shell
# If you are familiar with Linux commands, writing a shell script is just combining those commands.
# A shell script reduces manual work and boosts productivity.
# Official definition
# The shell prompts for input, passes it to the OS, and displays the result. It acts as a command interpreter.
# Common shells
# Bourne Shell (/usr/bin/sh), Bourne Again Shell (/bin/bash), C Shell (/usr/bin/csh), K Shell (/usr/bin/ksh), Root Shell (/sbin/sh)
# Bash is the most widely used due to its ease of use and free availability.1.2 Shell Programming Guidelines
Shell script naming : Use English letters, case‑sensitive, end with .sh Avoid special symbols and spaces
The script should start with #!/bin/bash Variable names cannot start with digits or special symbols; underscores are allowed, hyphens are not.
1.3 First Shell Script – Hello World
Create a simple Hello World script
# Create HelloWorld.sh
# touch HelloWorld.sh
# vim HelloWorld.sh
# cat HelloWorld.sh
#!/bin/bash
# This is our first shell script
# by author rivers 2021.09
echo "hello world"
# Make it executable
# chmod o+x HelloWorld.sh
# Run the script
# ./HelloWorld.sh
# Output: hello world2 Shell Environment Variables
2.1 Variable Basics
Environment variable introduction
# What is a variable
# A variable holds a value that can change, e.g., a=1, a=2, a=3.
# Three types of variables in shell
# System variables, environment variables, and user variables.
# Variable names must start with a letter, can contain underscores, but not digits at the start or hyphens.
# Simple variable example
# a=18
# echo $a # prints 182.2 System Variables
# Common system variables
$0 # script name
$n # nth argument
$* # all arguments
$# # number of arguments
$? # exit status of last command
$$ # PID of the script2.3 Environment Variables
Common environment variables
# PATH – command search path
# HOME – user home directory
# SHELL – current shell type
# USER – current username
# ID – user ID
# PWD – present working directory
# TERM – terminal type
# HOSTNAME – host name
# PS1 – command prompt definition
# HISTSIZE – history size
# RANDOM – random integer 0‑327672.4 User‑Defined Variables
Custom variables
# Example custom variables
a=rivers
Httpd_sort=httpd-2.4.6-97.tar
BACK_DIR=/data/backup/
IPaddress=10.0.0.13 Shell Control Flow Statements
3.1 if Conditional Statements
Basic if syntax
# Single‑branch if
if (condition); then
command1
fi
# Double‑branch if‑else
if (condition); then
command1
else
command2
fi
# Multi‑branch if‑elif‑else
if (condition); then
command1
elif (condition); then
command2
else
command3
fiCommon test operators
-f # file exists
-d # directory exists
-eq # integer equal
-ne # integer not equal
-lt # less than
-gt # greater than
-a # logical AND
-o # logical OR
-z # empty string
-x # executable permission
|| # OR (command level)
&& # AND (command level)3.2 for Loops
Loop over a list
# Syntax: for var in list; do ...; done
for i in $(seq 1 5); do
echo $i
doneCheck multiple hosts in a LAN
#!/bin/bash
Network=$1
for Host in $(seq 1 254); do
ping -c 1 $Network.$Host > /dev/null && result=0 || result=1
if [ "$result" == 0 ]; then
echo -e "\033[32;1m$Network.$Host is up\033[0m"
echo "$Network.$Host" >> /tmp/up.txt
else
echo -e "\033[31m$Network.$Host is down\033[0m"
echo "$Network.$Host" >> /tmp/down.txt
fi
done3.3 while Loops
Basic while syntax
# while (condition); do ...; done
while true; do
let N++
if [ $N -eq 5 ]; then break; fi
echo $N
doneSum 1‑100 using while
#!/bin/bash
j=0
i=1
while ((i<=100)); do
j=$((i + j))
((i++))
done
echo $j3.4 case Statements
Multiple choice handling
# Syntax
case $var in
pattern1) command;;
pattern2) command;;
*) default command;;
esacExample: HTTP service control
#!/bin/bash
while true; do
echo -e "
start
stop
status
quit"
read -p "Enter your choice: " char
case $char in
start) systemctl start httpd && echo "httpd started" || echo "start failed";;
stop) systemctl stop httpd && echo "httpd stopped" || echo "stop failed";;
status) systemctl status httpd;;
quit) exit;;
*) echo "Invalid option";;
esac
done3.5 select Menus
Menu‑driven selection
#!/bin/bash
PS3="Select a number: "
while true; do
select option in http php mysql quit; do
case $option in
http) echo "Test Httpd"; break;;
php) echo "Test PHP"; break;;
mysql) echo "Test MySQL"; break;;
quit) exit;;
*) echo "Input error, try again!"; break;;
esac
done
done4 Shell Functions and Arrays
4.1 Functions
Define reusable code blocks
# Function syntax
my_func() {
local result=$((1+1))
echo "This is a function."
return $result
}
my_func
echo $?4.2 Arrays
Store ordered collections
# Define an array
IP=(10.0.0.1 10.0.0.2 10.0.0.3)
# Iterate using index
for ((i=0;i<${#IP[@]};i++)); do
echo ${IP[$i]}
done
# Iterate directly
for ip in "${IP[@]}"; do
echo $ip
done5 Practical Shell Scripts
5.1 System Backup Script
#!/bin/bash
# Auto backup Linux system files
SOURCE_DIR=($*)
TARGET_DIR=/data/backup
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
WEEK=$(date +%u)
TIME=$(date +%H%M)
FILES=system_backup.tgz
if [ -z "$*" ]; then
echo -e "Usage: $0 /boot /etc"
exit 1
fi
if [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ]; then
mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
fi
Full_Backup() {
if [ "$WEEK" -eq 7 ]; then
rm -rf $TARGET_DIR/snapshot
cd $TARGET_DIR/$YEAR/$MONTH/$DAY && tar -g $TARGET_DIR/snapshot -czvf $FILES ${SOURCE_DIR[@]}
fi
}
Add_Backup() {
if [ "$WEEK" -ne 7 ]; then
cd $TARGET_DIR/$YEAR/$MONTH/$DAY && tar -g $TARGET_DIR/snapshot -czvf ${TIME}_$FILES ${SOURCE_DIR[@]}
fi
}
sleep 3
Full_Backup
Add_Backup5.2 Server Information Collection
# Collect system info and optionally store in MySQL
ip_info=$(ifconfig | grep "Bcast" | tail -1 | awk '{print $2}' | cut -d: -f2)
cpu_model=$(awk -F: '/model name/ {print $2}' /proc/cpuinfo | tail -1 | sed 's/^ //')
cpu_cores=$(awk '/physical id/ {print $4}' /proc/cpuinfo | sort -u | wc -l)
hostname=$(hostname)
disk=$(fdisk -l | grep "Disk" | grep -v "identifier" | awk '{print $2,$3,$4}' | tr -d ',')
mem=$(free -m | awk '/Mem/ {print "Total",$2"M"}')
load=$(uptime | awk -F'load average:' '{print $2}' | cut -d, -f1)
read -p "Write data to database? (yes/no): " ans
if [[ $ans == yes || $ans == y || $ans == Y ]]; then
mysql -uroot -p123456 -D audit -e "INSERT INTO audit_system VALUES('', '$ip_info', '$hostname', '$cpu_model X$cpu_cores', '$disk', '$mem', '$load', 'BeiJing_IDC');"
else
echo "Exit without storing data."
exit 0
fi5.3 One‑Click LNMP Deployment (nginx, mysql, php)
#!/bin/bash
# Simplified LNMP installer (selection menu)
PS3="Select component to install: "
select comp in nginx mysql php quit; do
case $comp in
nginx) echo "Installing nginx..."; # download, configure, make, install steps omitted for brevity;;
mysql) echo "Installing mysql..."; # download, configure, make, install steps omitted;;
php) echo "Installing php..."; # download, configure, make, install steps omitted;;
quit) exit;;
*) echo "Invalid choice";;
esac
doneSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
