Shell脚本作为一种强大的工具,被广泛应用于Linux和Unix系统中,它可以帮助我们自动化日常任务,提高工作效率。本篇文章将为你解析50个经典的Shell脚本编程案例,通过这些案例的学习,你将能够更好地掌握Shell脚本编程技巧。
1. 系统信息收集
案例描述:编写一个Shell脚本,用于收集系统的基本信息,如CPU型号、内存大小、硬盘使用情况等。
代码示例:
#!/bin/bash
echo "CPU型号:$(cat /proc/cpuinfo | grep "model name" | awk '{print $4}')"
echo "内存大小:$(free -m | grep "Mem" | awk '{print $2}' "M")"
echo "硬盘使用情况:"
df -h
2. 文件夹遍历
案例描述:编写一个Shell脚本,用于遍历指定文件夹下的所有文件和子文件夹。
代码示例:
#!/bin/bash
for dir in $(find /path/to/directory -type d)
do
echo "遍历:$dir"
ls -l $dir
done
3. 文本处理
案例描述:编写一个Shell脚本,用于统计一个文本文件中单词的个数。
代码示例:
#!/bin/bash
word_count=$(wc -w /path/to/textfile.txt)
echo "单词个数:$word_count"
4. 数据备份
案例描述:编写一个Shell脚本,用于自动备份指定文件夹的内容。
代码示例:
#!/bin/bash
backup_dir="/path/to/backup"
target_dir="/path/to/target"
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
rsync -avh /path/to/source "$backup_dir"
5. 系统监控
案例描述:编写一个Shell脚本,用于监控系统负载情况。
代码示例:
#!/bin/bash
while true
do
echo "当前负载:$(cat /proc/loadavg | awk '{print $1, $2, $3}')"
sleep 10
done
6. 网络请求
案例描述:编写一个Shell脚本,用于发送HTTP GET请求并获取网页内容。
代码示例:
#!/bin/bash
url="http://www.example.com"
response=$(curl -s "$url")
echo "网页内容:$response"
7. 日志分析
案例描述:编写一个Shell脚本,用于分析日志文件,提取有用的信息。
代码示例:
#!/bin/bash
log_file="/path/to/logfile.log"
search_term="error"
grep "$search_term" "$log_file" | awk '{print $1, $2, $3}'
8. 文件搜索
案例描述:编写一个Shell脚本,用于在指定文件夹中搜索特定文件。
代码示例:
#!/bin/bash
search_dir="/path/to/directory"
search_file="example.txt"
find "$search_dir" -name "$search_file"
9. 系统重启
案例描述:编写一个Shell脚本,用于重启系统。
代码示例:
#!/bin/bash
echo "系统即将重启,请保存所有工作..."
shutdown -r now
10. 自动部署
案例描述:编写一个Shell脚本,用于自动化部署应用程序。
代码示例:
#!/bin/bash
# 检查源代码是否存在
if [ ! -d "/path/to/source" ]; then
echo "源代码不存在,请检查..."
exit 1
fi
# 克隆源代码
git clone /path/to/source /path/to/deploy
# 安装依赖
cd /path/to/deploy
pip install -r requirements.txt
# 部署应用程序
python app.py
11. 数据同步
案例描述:编写一个Shell脚本,用于同步远程服务器上的数据到本地。
代码示例:
#!/bin/bash
remote_server="user@remote-server:/path/to/remote"
local_dir="/path/to/local"
rsync -avh "$remote_server" "$local_dir"
12. 文件加密
案例描述:编写一个Shell脚本,用于加密和解密文件。
代码示例:
#!/bin/bash
# 加密文件
echo "输入密码:"
read -s password
openssl enc -aes-256-cbc -salt -in /path/to/file -out /path/to/encrypted -pass pass:$password
# 解密文件
echo "输入密码:"
read -s password
openssl enc -aes-256-cbc -d -salt -in /path/to/encrypted -out /path/to/decrypted -pass pass:$password
13. 系统清理
案例描述:编写一个Shell脚本,用于清理系统中的临时文件。
代码示例:
#!/bin/bash
find /path/to/directory -type f -name "*.tmp" -delete
14. 网络诊断
案例描述:编写一个Shell脚本,用于检测网络连接状态。
代码示例:
#!/bin/bash
host="www.example.com"
ping -c 4 "$host" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "连接成功"
else
echo "连接失败"
fi
15. 日志滚动
案例描述:编写一个Shell脚本,用于实现日志文件滚动。
代码示例:
#!/bin/bash
log_file="/path/to/logfile.log"
max_size=10485760 # 10MB
if [ -f "$log_file" ]; then
current_size=$(stat -c %s "$log_file")
if [ $current_size -gt $max_size ]; then
mv "$log_file" "${log_file}_$(date +%Y%m%d%H%M%S)"
touch "$log_file"
fi
fi
16. 系统安全
案例描述:编写一个Shell脚本,用于检查系统中的安全漏洞。
代码示例:
#!/bin/bash
# 检查是否存在空密码
grep "^root:*$" /etc/shadow > /dev/null
if [ $? -eq 0 ]; then
echo "存在空密码"
fi
# 检查是否有可疑的文件或进程
sudo auditctl -w /path/to/suspected_file -p warx -k suspicious_file
sudo auditctl -w /path/to/suspected_process -p warx -k suspicious_process
17. 系统优化
案例描述:编写一个Shell脚本,用于优化系统性能。
代码示例:
#!/bin/bash
# 关闭不必要的系统服务
systemctl disable cups
systemctl disable bluetooth
systemctl disable hidd
# 清理缓存
rm -rf /var/lib/apt/lists/*
rm -rf /tmp/*
rm -rf /var/log/*
18. 网络监控
案例描述:编写一个Shell脚本,用于监控网络流量。
代码示例:
#!/bin/bash
while true
do
echo "当前流量:$(cat /proc/net/dev | grep eth0 | awk '{print $2, $10}')"
sleep 10
done
19. 系统备份
案例描述:编写一个Shell脚本,用于备份系统中的重要文件。
代码示例:
#!/bin/bash
backup_dir="/path/to/backup"
target_dir="/path/to/target"
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
tar czf "${backup_dir}/backup_$(date +%Y%m%d%H%M%S).tar.gz" /path/to/source
20. 文件压缩与解压
案例描述:编写一个Shell脚本,用于压缩和解压文件。
代码示例:
#!/bin/bash
# 压缩文件
tar czf /path/to/compressed.tar.gz /path/to/directory
# 解压文件
tar xzf /path/to/compressed.tar.gz -C /path/to/extracted
21. 系统时间设置
案例描述:编写一个Shell脚本,用于设置系统时间。
代码示例:
#!/bin/bash
# 设置系统时间为北京时间
date -s "2022-01-01 08:00:00"
hwclock -w
22. 系统更新
案例描述:编写一个Shell脚本,用于更新系统软件包。
代码示例:
#!/bin/bash
sudo apt-get update
sudo apt-get upgrade
23. 系统防火墙设置
案例描述:编写一个Shell脚本,用于设置系统防火墙规则。
代码示例:
#!/bin/bash
# 允许SSH连接
sudo ufw allow ssh
# 允许HTTP连接
sudo ufw allow http
# 允许HTTPS连接
sudo ufw allow https
24. 系统重启与关机
案例描述:编写一个Shell脚本,用于重启和关机系统。
代码示例:
#!/bin/bash
# 重启系统
sudo shutdown -r now
# 关机系统
sudo shutdown -h now
25. 系统分区管理
案例描述:编写一个Shell脚本,用于管理系统分区。
代码示例:
#!/bin/bash
# 创建分区
fdisk /dev/sda << EOF
n
p
1
t
83
w
EOF
# 格式化分区
mkfs.ext4 /dev/sda1
# 挂载分区
mount /dev/sda1 /mount/point
26. 系统磁盘分区
案例描述:编写一个Shell脚本,用于检查磁盘分区情况。
代码示例:
#!/bin/bash
df -h
27. 系统用户管理
案例描述:编写一个Shell脚本,用于创建和删除系统用户。
代码示例:
#!/bin/bash
# 创建用户
sudo useradd -m -d /home/user -s /bin/bash user
# 删除用户
sudo userdel user
28. 系统服务管理
案例描述:编写一个Shell脚本,用于启动、停止和重启系统服务。
代码示例:
#!/bin/bash
# 启动服务
sudo systemctl start service_name
# 停止服务
sudo systemctl stop service_name
# 重启服务
sudo systemctl restart service_name
29. 系统环境变量管理
案例描述:编写一个Shell脚本,用于查看和设置系统环境变量。
代码示例:
#!/bin/bash
# 查看环境变量
env
# 设置环境变量
export PATH=$PATH:/path/to/new
30. 系统文件权限管理
案例描述:编写一个Shell脚本,用于设置文件权限。
代码示例:
#!/bin/bash
# 设置文件权限
chmod 755 /path/to/file
# 设置文件所有者和组
chown user:user /path/to/file
# 设置文件所属组
chgrp group /path/to/file
31. 系统日志管理
案例描述:编写一个Shell脚本,用于查看和清理系统日志。
代码示例:
#!/bin/bash
# 查看系统日志
cat /var/log/syslog
# 清理系统日志
sudo logrotate /etc/logrotate.conf
32. 系统网络配置
案例描述:编写一个Shell脚本,用于查看和修改系统网络配置。
代码示例:
#!/bin/bash
# 查看网络配置
ifconfig
# 修改网络配置
sudo vi /etc/network/interfaces
33. 系统软件包管理
案例描述:编写一个Shell脚本,用于安装和卸载软件包。
代码示例:
#!/bin/bash
# 安装软件包
sudo apt-get install package_name
# 卸载软件包
sudo apt-get remove package_name
34. 系统备份与恢复
案例描述:编写一个Shell脚本,用于备份和恢复系统数据。
代码示例:
#!/bin/bash
# 备份系统数据
tar czf /path/to/backup.tar.gz /path/to/source
# 恢复系统数据
tar xzf /path/to/backup.tar.gz -C /path/to/target
35. 系统磁盘分区表备份
案例描述:编写一个Shell脚本,用于备份磁盘分区表。
代码示例:
#!/bin/bash
dd if=/dev/sda of=/path/to/partition_table_backup bs=512 count=1
36. 系统磁盘分区表恢复
案例描述:编写一个Shell脚本,用于恢复磁盘分区表。
代码示例:
#!/bin/bash
dd if=/path/to/partition_table_backup of=/dev/sda bs=512 count=1
37. 系统软件包升级
案例描述:编写一个Shell脚本,用于升级系统软件包。
代码示例:
#!/bin/bash
sudo apt-get update
sudo apt-get dist-upgrade
38. 系统软件包降级
案例描述:编写一个Shell脚本,用于降级系统软件包。
代码示例:
#!/bin/bash
sudo apt-get install package_name=package_version
39. 系统软件包源码编译安装
案例描述:编写一个Shell脚本,用于从源码编译安装软件包。
代码示例:
#!/bin/bash
# 下载源码
wget http://example.com/software.tar.gz
# 解压源码
tar xzf software.tar.gz
# 进入源码目录
cd software
# 配置编译参数
./configure --prefix=/usr/local
# 编译
make
# 安装
sudo make install
40. 系统软件包卸载
案例描述:编写一个Shell脚本,用于卸载系统软件包。
代码示例:
#!/bin/bash
# 卸载软件包
sudo apt-get remove package_name
41. 系统软件包查询
案例描述:编写一个Shell脚本,用于查询系统软件包信息。
代码示例:
#!/bin/bash
# 查询软件包信息
apt-cache show package_name
42. 系统软件包依赖关系
案例描述:编写一个Shell脚本,用于查看软件包的依赖关系。
代码示例:
#!/bin/bash
# 查看软件包依赖关系
apt-cache depends package_name
43. 系统软件包提供
案例描述:编写一个Shell脚本,用于查看软件包提供的功能。
代码示例:
#!/bin/bash
# 查看软件包提供的功能
apt-cache provides package_name
44. 系统软件包版本信息
案例描述:编写一个Shell脚本,用于查看软件包的版本信息。
代码示例:
#!/bin/bash
# 查看软件包版本信息
apt-cache showversion package_name
45. 系统软件包源码下载
案例描述:编写一个Shell脚本,用于下载软件包的源码。
代码示例:
#!/bin/bash
# 下载软件包源码
wget http://example.com/software.tar.gz
46. 系统软件包源码编译
案例描述:编写一个Shell脚本,用于编译软件包源码。
代码示例:
#!/bin/bash
# 解压源码
tar xzf software.tar.gz
# 进入源码目录
cd software
# 配置编译参数
./configure --prefix=/usr/local
# 编译
make
47. 系统软件包安装
案例描述:编写一个Shell脚本,用于安装软件包。
代码示例:
#!/bin/bash
# 安装软件包
sudo apt-get install package_name
48. 系统软件包升级
案例描述:编写一个Shell脚本,用于升级软件包。
代码示例:
#!/bin/bash
# 升级软件包
sudo apt-get upgrade package_name
49. 系统软件包降级
案例描述:编写一个Shell脚本,用于降级软件包。
代码示例:
#!/bin/bash
# 降级软件包
sudo apt-get install package_name=package_version
50. 系统软件包卸载
案例描述:编写一个Shell脚本,用于卸载软件包。
代码示例:
#!/bin/bash
# 卸载软件包
sudo apt-get remove package_name
以上50个经典Shell脚本编程案例涵盖了系统管理、文件处理、网络操作、软件包管理等各个方面。通过学习这些案例,你将能够更好地掌握Shell脚本编程技巧,并能够将其应用到实际工作中。
