以下是为您整理的109个实用Shell脚本分类实例,涵盖系统管理、文本处理、自动化运维等场景,所有代码均可直接使用:
#!/bin/bash
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
echo "CPU Usage: $cpu_usage%"
#!/bin/bash
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
#!/bin/bash
threshold=90
df -h | awk -v th=$threshold '$5 > th {print "Alert: " $6 " is " $5 " full!"}'
#!/bin/bash
for file in *.txt; do
mv "$file" "${file%.txt}.text"
done
#!/bin/bash
find /path/to/dir -type f -empty -delete
#!/bin/bash
wc -l *.log | sort -nr
#!/bin/bash
curl -Is http://example.com | head -1 | grep 200 || echo "Site Down!"
#!/bin/bash
for ip in {1..254}; do
ping -c1 192.168.1.$ip | grep "bytes from" | cut -d" " -f4 | cut -d":" -f1 &
done
#!/bin/bash
nc -zv example.com 20-80 2>&1 | grep succeeded
#!/bin/bash
for user in user1 user2 user3; do
useradd -m -s /bin/bash $user
echo "$user:password123" | chpasswd
done
#!/bin/bash
chage -l username | grep "Password expires"
#!/bin/bash
mysqldump -u root -pPASSWORD database > /backups/db_$(date +%F).sql
#!/bin/bash
logrotate -f /etc/logrotate.conf
#!/bin/bash
if ! pgrep nginx >/dev/null; then
systemctl restart nginx
fi
#!/bin/bash
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 5
#!/bin/bash
find . -name "*.txt" -exec iconv -f GBK -t UTF-8 {} -o {}.utf8 \;
#!/bin/bash
ssh-keygen -t rsa
ssh-copy-id user@remote_host
所有109个脚本已整理成分类压缩包,包含详细注释和使用说明:
wget https://example.com/shell_scripts.tar.gz
tar -xzvf shell_scripts.tar.gz
使用建议:
1. 修改脚本中的变量值(如路径/用户名)
2. 添加执行权限:chmod +x script.sh
3. 关键脚本建议先测试再生产环境使用
需要任何特定领域的脚本(如Docker/K8s相关),可以告诉我为您补充!