优秀脚本范例(优秀脚本范例怎么写)
6.1 信号捕捉 trap
trap '触发指令' 信号
进程收到系统发出的指定信号后,将执行自定义指令,而不会执行原操作
trap '' 信号
忽略信号的操作
trap '-' 信号
恢复原信号的操作
trap -p
列出自定义信号操作
trap fifinish EXIT
当脚本退出时,执行fifinish函数
范例:
#!/bin/bashtrap 'echo "Press ctrl+c"' int quittrap -pfor((i=0;i<=10;i++))do sleep 1 echo $idonetrap '' int trap -pfor((i=11;i<=20;i++))do sleep 1 echo $idonetrap '-' int trap -pfor((i=21;i<=30;i++))do sleep 1 echo $idone
范例:
[root@centos8 script40]#cat trap_exit.sh #!/bin/bash##********************************************************************#Author: wangxiaochun#QQ: 29308620#Date: 2020-01-06#FileName: trap_exit.sh#URL: http://www.magedu.com#Description: The test script#Copyright (C): 2020 All rights reserved#********************************************************************finish(){ echo finish| tee -a /root/finish.log}trap finish exitwhile true ;do echo running sleep 1done
6.2 创建临时文件 mktemp
mktemp 命令用于创建并显示临时文件,可避免冲突
格式
mktemp [OPTION]... [TEMPLATE]
说明:TEMPLATE: fifilenameXXX,X至少要出现三个
常见选项:
-d 创建临时目录
-p DIR或–tmpdir=DIR 指明临时文件所存放目录位置
范例:
mktemp /tmp/testXXXtmpdir=`mktemp –d /tmp/testdirXXX`mktemp --tmpdir=/testdir testXXXXXX
6.3 安装复制文件 install
install命令格式:
install [OPTION]... [-T] SOURCE DEST 单文件install [OPTION]... SOURCE... DIRECTORYinstall [OPTION]... -t DIRECTORY SOURCE...install [OPTION]... -d DIRECTORY...创建空目录
选项:
-m MODE,默认755
-o OWNER
-g GROUP
范例:
install -m 700 -o wang -g admins srcfile desfileinstall –m 770 –d /testdir/installdir
6.4 交互式转化批处理工具 expect
expect 是由Don Libes基于Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助 expect 处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率
expect 语法:
expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
常见选项:
-c:从命令行执行expect脚本,默认expect是交互地执行的
-d:可以输出输出调试信息
示例:
expect -c 'expect "n" {send "pressed entern"}'expect -d ssh.exp
expect中相关命令
- spawn 启动新的进程
- expect 从进程接收字符串
- send 用于向进程发送字符串
- interact 允许用户交互
- exp_continue 匹配多个字符串在执行动作后加此命令
expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
[root@centos8 test]#expectexpect1.1> expect "hi" {send "You said hin"}hahahixixiYou said hi
匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:
[root@centos8 test]#expectexpect1.1> expect "hi" { send "You said hin" } "hehe" { send "Hehe yourselfn" } "bye" { send "Good byen" }heheHehe yourselfexpect1.2> expect "hi" { send "You said hin" } "hehe" { send "Hehe yourselfn" } "bye" { send "Good byen" }byeGood byeexpect1.3> expect "hi" { send "You said hin" } "hehe" { send "Hehe yourselfn" } "bye" { send "Good byen" }hiYou said hiexpect1.4>
匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:
expect { "hi" { send "You said hin"} "hehe" { send "Hehe yourselfn"} "bye" { send " Good byen"}}
范例1:
#!/usr/bin/expectspawn scp /etc/fstab 10.0.0.7:/dataexpect { "yes/no" { send "yesn";exp_continue } "password" { send "magedun" }}expect eof
范例2:
#!/usr/bin/expectspawn ssh 10.0.0.7expect { "yes/no" { send "yesn";exp_continue } "password" { send "magedun" }}interact
范例3:expect 变量
#!/usr/bin/expectset ip 10.0.0.7set user rootset password mageduset timeout 10spawn ssh $user@$ipexpect { "yes/no" { send "yesn";exp_continue } "password" { send "$passwordn" }}interact
范例4:expect 位置参数
#!/usr/bin/expectset ip [lindex $argv 0] set user [lindex $argv 1]set password [lindex $argv 2]spawn ssh $user@$ipexpect { "yes/no" { send "yesn";exp_continue } "password" { send "$passwordn" }}interact#./ssh3.exp 192.168.8.10 root magedu
范例5:expect 执行多个命令
#!/usr/bin/expectset ip [lindex $argv 0] set user [lindex $argv 1]set password [lindex $argv 2]set timeout 10spawn ssh $user@$ipexpect { "yes/no" { send "yesn";exp_continue } "password" { send "$passwordn" }}expect "]#" { send "useradd hahan" }expect "]#" { send "echo magedu |passwd --stdin hahan" }send "exitn"expect eof#./ssh4.exp 10.0.0.7 root magedu
范例6:shell脚本调用expect
#!/bin/baship=$1 user=$2password=$3expect <<EOFset timeout 20spawn ssh $user@$ipexpect { "yes/no" { send "yesn";exp_continue } "password" { send "$passwordn" }}expect "]#" { send "useradd hehen" }expect "]#" { send "echo magedu |passwd --stdin hehen" }expect "]#" { send "exitn" }expect eofEOF #./ssh5.sh 192.168.8.10 root magedu
范例7: shell脚本利用循环调用expect在CentOS和Ubuntu上批量创建用户
#!/bin/bash##********************************************************************#Author: wangxiaochun#QQ: 29308620#Date: 2020-01-06#FileName: expect6.sh#URL: http://www.magedu.com#Description: The test script#Copyright (C): 2020 All rights reserved#********************************************************************NET=10.0.0user=rootpassword=magedufor ID in 6 7 111;doip=$NET.$IDexpect <<EOFset timeout 20spawn ssh $user@$ipexpect { "yes/no" { send "yesn";exp_continue } "password" { send "$passwordn" }}expect "#" { send "useradd testn" }expect "#" { send "exitn" }expect eofEOFdone
本文为《企业级shell脚本编程实战》系列教程第六部分,作者为Linux段子手王晓春老师,上一篇见文末扩展链接,后续会分享更多shell脚本编程知识,感兴趣的朋友可以关注下!
【电子版领取见下图!!】
如发现本站有涉嫌抄袭侵权/违法违规等内容,请联系我们举报!一经查实,本站将立刻删除。