当前位置: 首页 > news >正文

第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="recipient@example.com"
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"
sendmailrecipient@example.com<<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"
ifdiff$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
http://www.lryc.cn/news/44635.html

相关文章:

  • 【致敬嵌入式攻城狮第2期活动预热征文】学习安排
  • 035:cesium加载KML文件,显示图形
  • 随想录Day42--动态规划: 416. 分割等和子集(终于吃下01背包了)
  • 字节跳动软件测试岗,前两面过了,第三面被面试官吊打,结局我哭了
  • bitlocker 笔记
  • Linux 压缩与解压命令
  • python global函数用法及常用的 global函数代码
  • 大数据学完好就业么
  • CASAtomic 原子操作详解
  • 卷积神经网络(convolutional neural network, CNN)
  • kube-apiserver启动流程源码分析
  • Scala基础(二)
  • Python 生产者消费者模型是什么?
  • 手机银行评测系列:北京银行“京彩生活”7.0从用户视角出发,实现沉浸式体验重塑
  • ZJYC2023 浙江省大学生程序设计竞赛校内选拔赛部分题解 C J B L
  • 百科创建:7种有效的百科词条创建技巧
  • ThreeJS-dat.gui界面控制颜色、隐藏、位置(六)
  • 接口自动化测试,完整入门篇
  • 利用ControlNet重新定义你的AI姿势
  • 中医药NER命名实体识别基于SPANNER方式
  • Vue必掌握
  • SSM部分
  • 【Springboot系列】Springboot接管所有Controller,magic-api源码阅读
  • 二、LED子系统数据结构详解
  • Kubernetes(11):数据存储详解
  • 随想录Day43--动态规划: 1049. 最后一块石头的重量 II , 494. 目标和 , 474.一和零
  • Qt中对TCP粘包的处理
  • 贪心-单调递增的数字
  • 你真的会用搜索引擎吗?
  • KDCJ-20kV冲击耐压测试仪