Shell脚本是一种强大的工具,它可以帮助我们自动化日常的系统管理和任务执行。掌握Shell脚本,不仅能提高工作效率,还能在遇到系统问题时快速定位并解决问题。本篇文章将为你介绍50个实用的Shell脚本实战案例,帮助你轻松解决系统问题。
1. 检查磁盘空间
#!/bin/bash
# 检查根目录磁盘空间
df -h /
2. 定期清理日志文件
#!/bin/bash
# 定期清理系统日志文件
find /var/log -type f -mtime +7 -exec rm {} \;
3. 查找大文件
#!/bin/bash
# 查找当前目录下大于1GB的文件
find . -type f -size +1G
4. 自动备份重要文件
#!/bin/bash
# 自动备份/home目录下的重要文件到备份目录
tar -czvf /path/to/backup/home_backup_$(date +%Y%m%d).tar.gz /home
5. 检查服务状态
#!/bin/bash
# 检查Apache服务状态
service httpd status
6. 自动重启服务
#!/bin/bash
# 自动重启Apache服务
service httpd restart
7. 监控CPU和内存使用情况
#!/bin/bash
# 查看CPU和内存使用情况
top
8. 查找重复文件
#!/bin/bash
# 查找当前目录下重复的文件
find . -type f -print0 | xargs -0 du -h | sort -rh | head -n 20
9. 自动安装软件包
#!/bin/bash
# 自动安装软件包
sudo apt-get install -y package_name
10. 查看系统信息
#!/bin/bash
# 查看系统信息
hostname
cat /etc/os-release
11. 网络流量监控
#!/bin/bash
# 监控网络流量
iftop
12. 定时任务管理
#!/bin/bash
# 添加定时任务
crontab -e
# 每天凌晨1点执行备份脚本
0 1 * * * /path/to/backup_script.sh
13. 远程登录
#!/bin/bash
# 远程登录服务器
ssh user@remote_server_ip
14. SSH无密码登录
#!/bin/bash
# 生成SSH密钥对
ssh-keygen -t rsa -b 4096
# 将公钥复制到远程服务器
ssh-copy-id user@remote_server_ip
15. 查找进程
#!/bin/bash
# 查找占用CPU最多的进程
ps -ef | grep -v grep | sort -nr -k3 | head -n 10
16. 杀死进程
#!/bin/bash
# 杀死占用CPU最多的进程
ps -ef | grep -v grep | sort -nr -k3 | head -n 1 | awk '{print $2}' | xargs kill -9
17. 解压文件
#!/bin/bash
# 解压tar.gz文件
tar -zxvf file.tar.gz
18. 压缩文件
#!/bin/bash
# 压缩tar.gz文件
tar -czvf file.tar.gz file1 file2
19. 查找文本
#!/bin/bash
# 在文件中查找文本
grep "keyword" file.txt
20. 替换文本
#!/bin/bash
# 在文件中替换文本
sed -i 's/old_text/new_text/g' file.txt
21. 网络带宽测试
#!/bin/bash
# 测试网络带宽
speedtest-cli
22. 查看防火墙规则
#!/bin/bash
# 查看防火墙规则
iptables -L
23. 配置防火墙规则
#!/bin/bash
# 配置防火墙规则
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
24. 网络端口监控
#!/bin/bash
# 监控网络端口
netstat -tulnp | grep "80"
25. 查看系统负载
#!/bin/bash
# 查看系统负载
uptime
26. 磁盘分区
#!/bin/bash
# 创建新分区
fdisk /dev/sda
27. 磁盘格式化
#!/bin/bash
# 格式化磁盘
mkfs.ext4 /dev/sda1
28. 查看文件权限
#!/bin/bash
# 查看文件权限
ls -l file.txt
29. 修改文件权限
#!/bin/bash
# 修改文件权限
chmod 755 file.txt
30. 创建用户
#!/bin/bash
# 创建用户
useradd new_user
31. 删除用户
#!/bin/bash
# 删除用户
userdel old_user
32. 重置用户密码
#!/bin/bash
# 重置用户密码
passwd old_user
33. 查看用户信息
#!/bin/bash
# 查看用户信息
id old_user
34. 查看进程信息
#!/bin/bash
# 查看进程信息
ps -ef | grep old_process
35. 杀死进程
#!/bin/bash
# 杀死进程
kill -9 pid
36. 查看网络接口信息
#!/bin/bash
# 查看网络接口信息
ifconfig
37. 配置网络接口
#!/bin/bash
# 配置网络接口
echo "auto eth0" >> /etc/network/interfaces
echo "iface eth0 inet dhcp" >> /etc/network/interfaces
38. 查看网络连接
#!/bin/bash
# 查看网络连接
netstat -tulnp | grep "80"
39. 配置DNS服务器
#!/bin/bash
# 配置DNS服务器
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
40. 查看磁盘使用情况
#!/bin/bash
# 查看磁盘使用情况
df -h
41. 查看文件系统类型
#!/bin/bash
# 查看文件系统类型
file /path/to/file
42. 查看文件系统挂载点
#!/bin/bash
# 查看文件系统挂载点
mount
43. 查看系统版本
#!/bin/bash
# 查看系统版本
cat /etc/os-release
44. 查看内核版本
#!/bin/bash
# 查看内核版本
uname -r
45. 查看系统架构
#!/bin/bash
# 查看系统架构
uname -m
46. 查看CPU信息
#!/bin/bash
# 查看CPU信息
cat /proc/cpuinfo
47. 查看内存信息
#!/bin/bash
# 查看内存信息
free -m
48. 查看系统进程
#!/bin/bash
# 查看系统进程
ps -ef
49. 查看系统服务
#!/bin/bash
# 查看系统服务
systemctl list-units --type=service
50. 查看系统日志
#!/bin/bash
# 查看系统日志
tail -f /var/log/messages
通过以上50个实战案例,相信你已经对Shell脚本有了更深入的了解。在实际工作中,你可以根据需要修改和组合这些案例,以满足你的需求。祝你学习愉快!
