第1-第20个高级shell程序

高级shell脚本

1.使用Shell脚本批量修改文件名

#!/bin/bash 
for 
	fi lein$(ls*.txt)
do
mv $file${file%%.*}.md
done

2.统计一个文本文件中某个单词出现的次数

#!/bin/bashword="example"
count=0
whilereadline
do
forwin$line
do
if["$w"="$word"]
then
count=$((count+1))
fi
done
done<file.txt
echo"$wordappears$counttimes"

3.使用Shell脚本自动备份MySQL数据库

#!/bin/bashuser="username"
password="password"
host="localhost"
db_name="database"
backup_path="/path/to/backup"
date=$(date+"%Y-%m-%d")
mysqldump--user=$user--password=$password--host=$host$db_name>$backup_path/$db_name-$date.sql

4.使用Shell脚本实现文件压缩和解压

#!/bin/bash#压缩文件zip-rarchive.zipfolder/
#解压文件unziparchive.zip-ddestination_folder/

5.使用Shell脚本监控服务器CPU和内存使用情况

#!/bin/bashcpu_threshold=80
mem_threshold=80
whiletrue
do
cpu_usage=$(top-bn1|grep"Cpu(s)"|awk'{print$2}')
mem_usage=$(free|awk'/Mem/{print$3/$2*100}')
if[[$(echo"$cpu_usage>$cpu_threshold"|bc)-ne0]]
then
echo"CPUusageishigh:$cpu_usage"
fi
if[[$(echo"$mem_usage>$mem_threshold"|bc)-ne0]]
then
echo"Memoryusageishigh:$mem_usage"
fi
sleep5
done

6.使用Shell脚本实现FTP文件上传和下载

#!/bin/bash#FTP文件上传ftp-n<<EOF
openftp.example.com
userusernamepassword
putfile.txt
bye
EOF
#FTP文件下载ftp-n<<EOF
openftp.example.com
userusernamepassword
getfile.txt
bye
EOF

7.使用Shell脚本实现进程监控和重启

#!/bin/bashprocess_name="example"
whiletrue
do
ifpgrep$process_name>/dev/null
then
echo"$process_nameisrunning"
else
echo"$process_nameisnotrunning,restarting..."
/etc/init.d/$process_namerestart
fi
sleep10
done

8.使用Shell脚本实现定时任务

#!/bin/bash#每天凌晨1点执行任务01***/path/to/script.sh

9.使用Shell脚本实现日志分析

#!/bin/bashlog_file="/path/to/log/file"
whiletrue
do
tail-n0-f$log_file|whilereadline
do
ifecho"$line"|grep"error">/dev/null
then
echo"$line">>error.log
fi
done
sleep1
done

10.使用Shell脚本实现SSH登录自动化

#!/bin/bash#自动登录远程服务器并执行命令sshuser@remote_server'command'
#自动上传文件到远程服务器scpfile.txtuser@remote_server:/path/to/destination
#自动下载文件到本地scpuser@remote_server:/path/to/file.txt/path/to/destination

11.使用Shell脚本实现Git代码自动部署

#!/bin/bash#同步代码gitpull
#安装依赖npminstall
#构建项目npmrunbuild
#备份原有部署mv/path/to/deploy/path/to/deploy_backup
#将构建后的代码复制到部署目录cp-rdist/path/to/deploy
#重启服务systemctlrestartservice_name

12.使用Shell脚本实现邮件发送

#!/bin/bashto="[email protected]"
subject="Emailsubject"
body="Emailbody"
echo"$body"|mail-s"$subject""$to"

13.使用Shell脚本实现文件加密和解密

#!/bin/bash#加密文件opensslaes-256-cbc-salt-infile.txt-outfile.enc
#解密文件opensslaes-256-cbc-d-infile.enc-outfile.txt

14.使用Shell脚本实现网络监控和报警

#!/bin/bashhost="example.com"
whiletrue
do
if!ping-c1$host>/dev/null
then
echo"Hostisdown:$host"
[email protected]<<EOF
Subject:Hostisdown
$hostisnotrespondingtopings.
EOF
fi
sleep10
done

15.使用Shell脚本实现密码生成器

#!/bin/bashlength=16
nums='0123456789'
lower='abcdefghijklmnopqrstuvwxyz'
upper='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
special='!@#$%^&*()_+-=[]{}|;:,.<>?'
characters="$nums$lower$upper$special"
password=$(echo$characters|fold-w1|shuf|head-c"$length")
echo"$password"

16.使用Shell脚本实现日志轮转

#!/bin/bash
log_file="/path/to/log/file"
max_size=10000000
backups=5
if[-s$log_file]
then
size=$(du-b$log_file|awk'{print$1}')
if[$size-gt$max_size]
then
for((i=$backups-1;i>=0;i--))
do
if[-f$log_file.$i]
then
mv$log_file.$i$log_file.$((i+1))
fi
done
mv$log_file$log_file.0
touch$log_file
fi
fi

17.使用Shell脚本实现进程限制

#!/bin/bashmax_processes=5
whiletrue
do
if[$(pgrep-cprocess_name)-lt$max_processes]
then
./process_name&
fi
done

18.使用Shell脚本实现远程命令执行

#!/bin/bash
ssh user@remote_server'command'

19.使用Shell脚本实现文件比较

#!/bin/bash
file1="/path/to/file1"
file2="/path/to/file2"
if
	diff$file1$file2>/dev/null
then
echo"File sareidentical"
else
echo"File saredifferent"
fi

20.使用Shell脚本实现用户管理

#!/bin/bash#添加新用户useraddnew_user
#删除用户
userdel old_user
#修改密码
passwd username
#切换用户
su username

猜你喜欢

转载自blog.csdn.net/qq_44652591/article/details/129816522