Shell面交互

Here Document概述

  • 使用I/O重定向的方式将命令列表提供给交互式程序
  • 标准输入的一种替代品
  • 语法格式
命令   <<标记
.....
.....
标记

Here Document使用注意事项

  • 标记可以使用任意合法字符
  • 结尾的标记一定要顶格写,前面不能有任何字符
  • 结尾的标记后面也不能有任何字符(包括空格)
  • 开头标记前后的空格会被省略掉

Here Document免交互

通过read命令接收输入并打印

[root@localhost ~]# vim here_non_interactive_read.sh
#!/bin/bash
read i <<EOFHi
EOF
echo $i
[root@localhost ~]# chmod +x here_non_interactive_read.sh
[root@localhost ~]# ./here_non_interactive_read.sh
[root@localhost ~]# Hi

通过passwd给用户设置密码

[root@localhost ~]# vim here_non_interactive_passwd.sh
#!/bin/bash
passwd jerry <<EOF
This_is_password 		#passwd命令需要的密码和确认密码
This_is_password
EOF
[root@localhost ~]# chmod +x here_non_interactive_passwd.sh
[root@localhost ~]#./here_non_interactive_passwd.sh
Here Document变量设定

变量替换

[root@localhost~]# vim here_var_replace.sh
#!/bin/bash
doc_ file="2019.txt"
i="company"
cat > $doc_file << HERE
Take him from home to $i
HERE
[root@localhost ~]# chmod +x here_var_replace.sh
[root@localhost ~]# ./here_var_replace.sh
[root@localhost ~]# cat 2019.txt
Take him from home to company

变量设定

[root@localhost ~]# vim here_var_set.sh
#!/bin/bash
ivar="Great! Beautyful!"
myvar=$(cat<<EOFThis is Line 1.
That are Sun,Moon and Stars.
$ivar
EOF
)
echo $myvar
[root@localhost ~]# sh here_var_set.sh
This is Line 1.That are Sun,Moon and Stars. Great! Beautyful!

Here Document格式控制

关闭变量替换功能

[root@localhost ~]# cat here_format_shut.sh
#!/bin/bash
cat <<'EOF'		#单引号关闭变量替换
This is Line 1.
$kgc
EOF
[root@localhost ~]# sh here_format_shut.sh
This is Line 1.
$kgc

去除每行之前的TAB字符

[root@localhost ~]# vim here_format tab.sh
#!/bin/bash
cat<<-'EOF'			#表示抑制行首的TAB作用
This is Line 1.
$kgc
EOF
[root@localhost ~]# sh here_format_tab.sh
This is Line 1.
$kac

Here Document多行注释

通过Here Document方式使Bash支持多行注释
语法格式

:<< DO-NOTHING
第一行注释
第二行注释
DO-NOTHING

Expect概述

Expect

  • 建立在tcl之上的一个工具
  • 用于进行自动化控制和测试
  • 解决shell脚本中交互相关的问题

Expect单一分支语法

expect "password: " {send "mypassword\r";}

Expect多一分支语法

方法一

expect "aaa" {send "AAA\r"}
expect "bbb" {send"BBB"\r}
expect "ccc" {send "CCC\r"}

send命令不具备回车换行功能,一般要加\r或\n
方法二

expect {
“aaa" {send "AAA\r"}
“bbb" {send "BBB\r}
"ccc" {send "CCC\r"}
}

只要些配了aaaobbbgccc甲的任何一个,执行相应的send语句后退出该expect语句
方法三

expect {
aaa" {send "AAA"; exp_continue}
“bbb" {send"BBB";exp_continue}
“ccc" {send "CCC"}

exp_continue示困实同间的四配,如果匹配了aaa,执行完send语句后还要继续向下匹配bbb

Expect执行方式

直接执行

[root@localhost ~]# more direct.sh
#!/usr/bin/expect		#Expect二进制文件的路径
set timeout 60
log _file test.log
log user 1
set hostname [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh root@$hostname
expect {
"(yes/no)"
{send "yesr"; exp_continue}
password"
{send "$password\r"}
}
lnteract
[root@localhost ~]# chmod a+x direct.sh
[root@localhost ~]#./direct.sh

嵌入执行

[root@localhost ~]# more implant.sh
#!/bin/bash
hostname=$1
password=$2
/usr/bin/expect<<-EOF		#Expect开始标志
spawn ssh root@${hostname}
expect {
"(yes/no)"
{send "yeslr ";exp_continue}"
"*password"
{send "$password\r"}
}
pect "*]#"
send "exit\r"
expect eof
EOF						#Expect结束标志,前后不能有空格
[root@localhost ~]# source implant.sh

Expect安装

  • 挂载光盘
  • 制作本地YUM源
  • 执行安装命令
[root@localhost ~]# yum -y install expect
[root@localhost~]# rpm -qalgrep expectexpect-5.45-14.el7_1.x86_64
[root@localhost ~]# rpm -qalgrep tcl
tcl-8.5.13-8.el7.x86_64

expect

  • 判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后回
  • 只能捕捉由spawn启动的进程的输出
  • 用于接收命令执行后的输出,然后和期望的字符串匹配

send

  • 向进程发送字符串,用于模拟用户的输入
  • 该命令不能自动回车换行,一般要加\r(回车)

spawn

  • 启动进程,并跟踪后续交互信息

结束符

expect eof #执行目动化任务通常使用expect eof
等待执行结束
interact
执行完成后保持交互状态,把控制权交给控制台

set

  • 设置超时时间,过期则继续执行后续指令
  • 单位是秒
  • timeout -1表示永不超时
  • 默认情况下,timeout是10秒

exp_continue

  • 允许expect继续向下执行指令

send_user

  • 回显命令,相当于echo

接收参数

  • Expect脚本可以接受从bash传递的参数
  • 可以使用[lindex $argv n]获得
  • n从0开始,分别表示第一个,第二个,第三个…参数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值