做信息采集的网站,注册城乡规划师2023年考试时间,建设工程业绩查询网站,广州网站建设品牌一、Here Document免交互
1#xff1a;概述
Here Document 是一个特殊用途的代码块#xff0c;它在 Linux Shell 中使用 I/O 重定向的方式将命令列表提供给交互式程序或命令#xff0c;比如 ftp、cat 或 read 命令#xff0c;Here Document 是标准输入的一种替代品
语法…一、Here Document免交互
1概述
Here Document 是一个特殊用途的代码块它在 Linux Shell 中使用 I/O 重定向的方式将命令列表提供给交互式程序或命令比如 ftp、cat 或 read 命令Here Document 是标准输入的一种替代品
语法格式命令 标记......标记
Here Document 也可以与非交互式程序和命令一起使用 标记可以使用任意的合法字符结尾的标记一定要顶格写前面不能有任何字符结尾的标记后面也不能有任何字符(包括空格)开头的标记前后的空格会被省略掉
//用wc -l的命令统计输入的文字的行数
//拓展wc -l可以显示行数需要参数;例如: ls | wc -l
[rootlocalhost~# vim here_wc_count.sh
#!/bin/bash
wc -l EOF //Here Document 语法结构
Line1 //EOF 之间是传入内容
Line2
EOF
[rootlocalhost ~]# chmod x here_wc_count.sh
[rootlocalhost ~]# ./here_wc_count.sh
2
2Here Document免交互
1利用read命令接受输入并打印 通常使用read 命令接收用户的输入值时会有交互过程尤其是在脚本执行过程中遇到read 命令脚本会停下来等待用户输入值后才会继续本示例中的输入值是两个 EOF 标记之间的部分也就是“Hi”这将作为变量的值。在最后 echo 打印变量i的值其值为“Hi”
[rootlocalhost ~]# vim here non interactive read.sh
#!/bin/bash
read iEOF
Hi
EOF
echo $i
[rootlocalhost ~]# chmod x here_non interactive_read.sh
[rootlocalhost ~]# ./here non interactive read.sh
[rootlocalhost ~]# Hi
2利用passwd给用户添加密码 通过 passwd 命令给 jery 用户设置密码为避免重复交互可使用 Here Document的方式EOF 标记之间的两行是输入的密码和确认密码两行内容必须保持一致否则密码设置不成功
[rootlocalhost ~# vim here non interactive passwd.sh
#!/bin/bash
passwd jerry EOF
This is password //这两行是输入密码和确认密码
This is password
EOF
[rootlocalhost ~]# chmod x here non interactive passwd.sh
[rootlocalhost ~]# ./here non interactive passwd.sh
[rootlocalhost ~]#
3Here Document变量设定
Here Document也支持使用变量如果标记之间有变量被使用会先替换变量值
[rootlocalhost ~]# vim here var replace.sh#!/bin/bash
doc file2019.txt
icompany
cat $doc file HERE
Take him from home to $i
HERE
[rootlocalhost ~]# chmod x here_var_replace.sh
[rootlocalhost~]# ./here_var_replace.sh
[rootlocalhost ~]# cat 2019.txt
Take him from home to company
标记内变量i的值被替换成了“company最终结果输出到$doc file 内其值为 2019.txt[rootlocalhost ~l# vim here var set.sh
#!/bin/bash
ivarGreat! Beautyfu!!
myvar$(cat EOF //将 Here Document 整体赋值给变量
This is Line 1.
That are Sun.Moon and Stars.
$ivar //输出时会进行变量替换
EOF
)
echo $myvar
[rootlocalhost ~l# sh here var set.sh
This is Line 1. That are Sun.Moon and Stars. Great! Beautyful!
$ivar 先进行了替换之后再转向输出交由cat 显示出来其结果放置到$0)中
4Here Document格式控制
1关闭变量替换功能
[rootlocalhost ~]# cat here format shut.sh
#!/bin/bash
cat EOF //对标记加单引号即可关闭变量替换
This is Line 1.
$kgc
EOF
[rootlocalhost ~l# sh here format shut.shThis is Line 1.
$kgc //$kgc 没有发生改变不做变量替换
2去掉每行之前的TAB字符
[rootlocalhost ~]# vim here format tab.sh
#!/bin/bash
cat -EOFThis is Line 1.$kgc
EOF
[rootlocalhost ~]# sh here format tab.shThis is Line 1.
$kgc //输出结果同上一示例
5Here Document多行注释
Bash 的默认注释是“#”该注释方法只支持单行注释在 Shel 脚本的工作中“#”右侧的任何字符串bash 都会将其忽略。Here Document的引入解决了多行注释的问题
: DO-NOTHING
第一行注释
第二行注释
……
DO-NOTHING
结构中:”代表什么都不做的空命令。中间标记区域的内容不会被执行会被bash 忽略掉因此可达到批量注释的效果[rootlocalhost ~]# vim here_multi.sh#!/bin/bash
:BASH-HERE //多行注释
the first comment.
the second comment.
test line.
BASH-HERE
echo exec string.
[rootlocalhost ~# sh here multi.sh
exec string.
脚本用于演示 Shel 中多行注释,“:”开头的 Here Document 标记内容不会被执行
二、expect免交互
1概述
建立在tcl之上的一个工具用于进行自动化控制和测试解决shell脚本中交互相关的问题
2expect安装
(1)挂载光盘
[rootlocalhost ~]# mount /dev/sr0 /media //通过 mount 命令挂载光盘到本地的/media 目录
(2)制作本地 YUM 源
[rootlocalhost ~# vim /etc/yum.repos.d/local.repo
namelocalrepo
baseurlfile:///media
gpgcheck0
//进入/etc/yum.repos.d目录删除默认存在的所有仓库配置文件新建文件并命名为local.repo其中后缀.repo 是必须的
[rootlocalhost ~]# yum clean all
[rootlocalhost ~]# yum make cache
//编写完配置文件后执行以下命令删除 yum 缓存并更新
(3)执行安装命令
[rootlocalhost ~]# yum -y install expect
3基本命令介绍
1脚本解释器
#!/usr/bin/expect
2expect/send
expect 命令用来判断上次输出结果里是否包含指定的字符串如果有则立即返回否则就等待超时时间后返回只能捕捉由spawn 启动的进程的输出expect 接收命令执行后的输出然后和期望字符串匹配若匹配成功则执行相应的send 向进程发送字符串用于模拟用户的输入。Send 发送的命令不能自动回车换行一般要加\r(回车)
方法一
expect $case1 {send $respond1\r}
方法二
expect $case1
send $response1\r
方法三
expect 支持多个分支
expect
{$case1{send $response1\r}$case2{send $response2\r}$case3{send $response3\r}
}
3spawn
spawn 后面通常跟一个命令表示开启一个会话、启动进程并跟踪后续交互信息
spawn Linux执行命令
想要跟踪切换用户的交互信息
spawn su root
4结束符
expect eof :等待执行结束若没有这一句可能导致命令还没执行脚本就结束了interact : 执行完成后保持交互状态把控制权交给控制台这时可以手动输入信息 需要注意的是expect eof 与 interact 只能二选一
5set
设置超时时间过期则继续执行后续指令单位是秒默认情况下timeout是10秒timeout -1表示永不超时
set timeout 30
6exp_continue
附加于某个expect判断项之后可以使该项被匹配后还能继续匹配该expect-判断语句内的其他项。exp_continmue类似于控制语句中的continue 语句。表示允许expect继续向下执行指令
7send_user
回显命令相当于echo
8接受参数
expect脚本可以接受从bash传递的参数可以使用[lindex %argv n]获得n从0开始分别表示第一个、第二个、第三个...参数
4expect语法
1、语法结构
1单一分支语法
单一分支用于简单的用户交互当监控命令的标准输出满足 expect 指定的字符串时,向标准输入发送 send 指ba定的字符串
expect password: {send mypassword\r;}
2多分支模式语法
多分支用于复杂的用户交互一般情况下输出内容可能有多个根据不同的输出内容分别向标准输入发送不同的内容
expect
{aaa{send AAA\r}bbb{send BBB\r}ccc{send CCC\r}
}
另一种的多分支结构
expect
{aaa{send AAA;exp_continue}bbb{send BBB;exp_continue}ccc{send CCC}
}
2、expect执行方式
1直接执行
[rootlocalhost ~]# more direct.sh
[rootlocalhost ~l# chmod x direct.sh
[rootlocalhost ~]# ./direct.sh 127.0.0.1 123456 //参数为主机ip 和密码
2嵌入执行
[rootlocalhost ~]# more implant.sh