第1-第20个高级shell程序

不念不忘少年蓝@ 2024-03-24 10:06 215阅读 0赞

高级shell脚本

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

  1. #!/bin/bash
  2. for
  3. fi lein$(ls*.txt)
  4. do
  5. mv $file${file%%.*}.md
  6. done

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

  1. #!/bin/bashword="example"
  2. count=0
  3. whilereadline
  4. do
  5. forwin$line
  6. do
  7. if["$w"="$word"]
  8. then
  9. count=$((count+1))
  10. fi
  11. done
  12. done<file.txt
  13. echo"$wordappears$counttimes"

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

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

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

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

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

  1. #!/bin/bashcpu_threshold=80
  2. mem_threshold=80
  3. whiletrue
  4. do
  5. cpu_usage=$(top-bn1|grep"Cpu(s)"|awk'{print$2}')
  6. mem_usage=$(free|awk'/Mem/{print$3/$2*100}')
  7. if[[$(echo"$cpu_usage>$cpu_threshold"|bc)-ne0]]
  8. then
  9. echo"CPUusageishigh:$cpu_usage"
  10. fi
  11. if[[$(echo"$mem_usage>$mem_threshold"|bc)-ne0]]
  12. then
  13. echo"Memoryusageishigh:$mem_usage"
  14. fi
  15. sleep5
  16. done

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

  1. #!/bin/bash#FTP文件上传ftp-n<<EOF
  2. openftp.example.com
  3. userusernamepassword
  4. putfile.txt
  5. bye
  6. EOF
  7. #FTP文件下载ftp-n<<EOF
  8. openftp.example.com
  9. userusernamepassword
  10. getfile.txt
  11. bye
  12. EOF

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

  1. #!/bin/bashprocess_name="example"
  2. whiletrue
  3. do
  4. ifpgrep$process_name>/dev/null
  5. then
  6. echo"$process_nameisrunning"
  7. else
  8. echo"$process_nameisnotrunning,restarting..."
  9. /etc/init.d/$process_namerestart
  10. fi
  11. sleep10
  12. done

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

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

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

  1. #!/bin/bashlog_file="/path/to/log/file"
  2. whiletrue
  3. do
  4. tail-n0-f$log_file|whilereadline
  5. do
  6. ifecho"$line"|grep"error">/dev/null
  7. then
  8. echo"$line">>error.log
  9. fi
  10. done
  11. sleep1
  12. done

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

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

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

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

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

  1. #!/bin/bashto="recipient@example.com"
  2. subject="Emailsubject"
  3. body="Emailbody"
  4. echo"$body"|mail-s"$subject""$to"

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

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

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

  1. #!/bin/bashhost="example.com"
  2. whiletrue
  3. do
  4. if!ping-c1$host>/dev/null
  5. then
  6. echo"Hostisdown:$host"
  7. sendmailrecipient@example.com<<EOF
  8. Subject:Hostisdown
  9. $hostisnotrespondingtopings.
  10. EOF
  11. fi
  12. sleep10
  13. done

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

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

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

  1. #!/bin/bash
  2. log_file="/path/to/log/file"
  3. max_size=10000000
  4. backups=5
  5. if[-s$log_file]
  6. then
  7. size=$(du-b$log_file|awk'{print$1}')
  8. if[$size-gt$max_size]
  9. then
  10. for((i=$backups-1;i>=0;i--))
  11. do
  12. if[-f$log_file.$i]
  13. then
  14. mv$log_file.$i$log_file.$((i+1))
  15. fi
  16. done
  17. mv$log_file$log_file.0
  18. touch$log_file
  19. fi
  20. fi

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

  1. #!/bin/bashmax_processes=5
  2. whiletrue
  3. do
  4. if[$(pgrep-cprocess_name)-lt$max_processes]
  5. then
  6. ./process_name&
  7. fi
  8. done

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

  1. #!/bin/bash
  2. ssh user@remote_server'command'

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

  1. #!/bin/bash
  2. file1="/path/to/file1"
  3. file2="/path/to/file2"
  4. if
  5. diff$file1$file2>/dev/null
  6. then
  7. echo"File sareidentical"
  8. else
  9. echo"File saredifferent"
  10. fi

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

  1. #!/bin/bash#添加新用户useraddnew_user
  2. #删除用户
  3. userdel old_user
  4. #修改密码
  5. passwd username
  6. #切换用户
  7. su username

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序编程20

    教程 接着上节课讲,这节课我们的目标就是把这个复利计算器能够算出结果并展示即可 由于开始计算按钮我们还没有绑定任何事件,我们需要给按钮绑定一个点击事件。 <b