首页 > 其他分享 >其他脚本相关的工具tarp、mktemp、install、 expect

其他脚本相关的工具tarp、mktemp、install、 expect

时间:2023-04-22 23:31:53浏览次数:37  
标签:mktemp ... 信号 send expect install root trap

一、信号捕捉tarp

trap '触发指令' 信号

进程收到系统发出的指定信号后,将执行自定义指令,而不会执行原操作

trap '' 信号

忽略信号的操作

trap '-' 信号

恢复原信号的操作

trap -p

列出自定义信号操作

trap finish EXIT

当脚本退出时,执行finish函数

trap '触发指令' 信号

进程收到系统发出的指定信号后,将执行自定义指令,而不会执行原操作

trap '' 信号

忽略信号的操作

trap '-' 信号

恢复原信号的操作

trap -p

列出自定义信号操作

trap finish EXIT

当脚本退出时,执行finish函数

 范例:

#!/bin/bash

trap 'echo "Press ctrl+c"'  int quit

trap -p

for((i=0;i<=10;i++))

do

sleep 1

echo $i

done

trap ''  int

trap -p

for((i=11;i<=20;i++))

do

sleep 1

echo $i

done

trap '-'  int

trap -p

for((i=21;i<=30;i++))

do

sleep 1

echo $i

done

二、 创建临时文件mktemp

mktemp可创建临时文件或者临时文件夹

格式:

mktemp [OPTION]... [TEMPLATE]

说明:TEMPLATE: filenameXXX,X至少要出现三个

常见选项:

-d 创建临时目录

-p DIR或--tmpdir=DIR 指明临时文件所存放目录位置

范例:

DIR=`mktemp -d /tmp/trash-$(date +%F_%H-%M-%S)XXXXXX`

mv $* $DIR

echo $* is move to $DIR

[root@centos8 ~]#alias rm=/data/scripts/rm.s

三、安装复制文件install

install 功能相当于cp,chmod,chown,chgrp 等相关工具的集合

 install命令格式:

install [OPTION]... [-T] SOURCE DEST 单文件

install [OPTION]... SOURCE... DIRECTORY

install [OPTION]... -t DIRECTORY SOURCE...

install [OPTION]... -d DIRECTORY...创建空目录


选项-m MODE,默认755

-o OWNER-g GROUP-d DIRNAME 目录 范例:

install -m 700 -o wang -g admins srcfile desfile

install –m 770 –d /testdir/installdir

[root@centos8 ~]#install -m 700 -o mage -g daemon -d /data/testdir

[root@centos8 ~]#ll -d /data/testdir

drwx------ 2 mage daemon 6 Apr 29 15:09 /data/testdi


四、交互式转换批处理工具expect

介绍:expect 是由Don Libes基于 Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式

操作的场景,借助 expect 处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本

上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人

员的工作效率

expect语法:

expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

expect  -c 'expect "\n" {send "pressed enter\n"}'

xpect  -d ssh.exp

expect中相关命令:

spawn

expect

send

interact

启动新的进程

从进程接收字符串

用于向进程发送字符串

允许用户交互

exp_continue 匹配多个字符串在执行动作后加此命令

expect最常用的语法(tcl语言:模式-动作)

范例:expect变量加位置参数

root@centos8 scripts]#cat expect4

#!/usr/bin/expect

set ip [lindex $argv 0]    #expect的变量要加set,后方[]里面相当于shell语言中位置变量的$1,

set user [lindex $argv 1]

set password [lindex $argv 2]

spawn ssh 其他脚本相关的工具tarp、mktemp、install、 expect_信号捕捉tarpip   

expect {

"yes/no" { send "yes\n";exp_continue }

"password" { send "$password\n" }

}

interact   #允许登录用户进行交互式操作

[root@centos8 scripts]#./expect4 10.0.0.7 root magedu

spawn ssh [email protected]

[email protected]'s password:

范例2:

NET=10.0.0

user=roo

tpassword=magedu

for ID in 6 7 111;do

 ip=$NET.$ID

expect <<EOF

set timeout 20

spawn ssh $user@$ip

expect {"yes/no" { send "yes\n";exp_continue}

"password" { send "$password\n" }

}
expect "#" { send "useradd test\n" }
expect "#" { send "exit\n" }
expect eof  #推出expect
EOF

done



标签:mktemp,...,信号,send,expect,install,root,trap
From: https://blog.51cto.com/gttwangyanjun/6215721

相关文章

  • mybatis-plus使用聚合函数报错---------net.sf.jsqlparser.parser.ParseException: En
    错误日志: Causedby:net.sf.jsqlparser.parser.ParseException:Encounteredunexpectedtoken:"with""WITH"atline62,column20.Wasexpectingoneof:"&""::"";""<<&q......
  • Invalid prop: type check failed for prop "defaultExpandAll". Expected Boolean, g
    vue中使用element-ui报错如下,defaultExpandAll关键词页面也搜不到[Vuewarn]:Invalidprop:typecheckfailedforprop"defaultExpandAll".ExpectedBoolean,gotStringwithvalue"true".foundin---><ElTable>atpackages/table/src/table.vue......
  • MySQL Shell 使用报错 SyntaxError: Unexpected identifier
    文章目录一、问题报错二、解决办法一、问题报错MySQLShell8.0.23Copyright(c)2016,2021,Oracleand/oritsaffiliates.OracleisaregisteredtrademarkofOracleCorporationand/oritsaffiliates.Othernamesmaybetrademarksoftheirrespectiveowners.T......
  • Installation failed with message Failed to establish session
    Androidstudio的setting里面build==》关闭instantrun用Androidstudio2.3调度程序时提示“InstallationfailedwithmessageFailedtoestablishsession”错误,需要在在开发者选项里关闭MIUI优化!开启手机开发者模式,在开发者选择中打开,USB安装(允许通过USB安装应用)......
  • Flink启动报错:/bin/config.sh: line 32: syntax error near unexpected token
    flink启动报错xxx@ssss:/xxx/flink-1.15.2/bin>shstart-cluster.sh/xxx/flink-1.15.2/bin/config.sh:line32:syntaxerrornearunexpectedtoken`<'/xxx/flink-1.15.2/bin/config.sh:line32:`done<<(find"$FLINK_LIB_DIR"!-ty......
  • grafana仪表盘的数据报错unexpected character: ‘\ufeff’”
    grafana仪表盘的数据拷贝展示后,出现报错:Parseerroratchar4:unexpectedcharacter:‘\ufeff’”报错时点击编辑仪表盘,发现没有什么异常的地方:这时可以点击jsons数据来查看是否存在了些特殊的不可见字符这里面一些不可见字符或者或展示出来,删除即可这是低版本的一个bug......
  • pyinstaller 打包时第三方模块与图片资源加载
    打包命令pyinstaller--onefile--windowed**.py 修改**.spec文件pathex=['/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ddddocr'],datas=[('./images','images'),('/Library/Frameworks/Python.fram......
  • org.pentaho.di.core.exception.KettleXMLException: Unexpected problem reading sh
    【kettle】【报错】UnexpectedproblemreadingsharedobjectsfromXMLfile当读共享文件时发生错误UnexpectedproblemreadingsharedobjectsfromXMLfile:null当读共享文件时发生错误kettle新建转换时,“读取共享对象时发生一个严重错误”“Unexpectedproblemre......
  • Install Ruby 1.9.3 with libyaml on CentOS
    评:,其一就是安装ruby和rubygem,为了方便起见这里推荐安装ruby1.9.2及之后的版本,这些版本已经包含了rubygem,无需单独安装,Ruby1.9.3-p0makespsych—thereplacementfor1.8.7’sYAMLlibrary,Syck—thedefaultYAMLparser.Psychisawrapperaroundlibyaml,soyou’reg......
  • chatgpt--mvn install 当做笔记保留
    在Maven中安装外部包需要使用`mvninstall:install-file`命令,其语法如下:mvninstall:install-file-Dfile=<path-to-file>\-DgroupId=<group-id>\-DartifactId=<artifact-id>\-Dversion=<version>\-Dpackaging=<packaging>\-Dg......