当前位置:网站首页>Shell script interaction free
Shell script interaction free
2022-04-23 10:03:00 【qq_ fifty-two million eight hundred and twenty-five thousand si】
Catalog
One 、Here Document No interaction
2、Here Document Precautions for use
One 、Here Document No interaction
1、 Command format
Use l/o Redirection provides a list of commands to interactive programs
A substitute for standard input
Command format
command << Mark
Content
Mark
2、Here Document Precautions for
(1) Tags can use any legal character
(2) The ending mark must be written in the top case , There can't be any characters in front of it .
(3) There can't be any characters after the tag at the end ( Including Spaces )
(4) Spaces before and after the opening mark are omitted
3、 example
(1) The number of rows can be counted without interaction
[root@localhost ~]# wc -l <<EOF
> 1
> 456
> 7810
> EOF
3

(2) adopt read Command receive command
[root@localhost ~]# read b <<EOF
> like
> EOF
[root@localhost ~]# echo $b
like

(3) No need to change password interactively
[root@localhost ~]# passwd zs <<EOF
> 123123
> 123123
> EOF
Change user zs Password .
new password : Invalid password : The password is less than 8 Characters
Reenter the new password :passwd: All authentication tokens have been successfully updated .

(4) When you write to a file, you first replace the variable with the actual value , combining cat Command complete output
#!/bin/bash
file="2.txt"
i=tea
cat> $file <<EOF
l like $i
EOF
[root@localhost ~]# cat 2.txt
l like tea

(5) Turn off the function of variable replacement , Output the characters as they are , Without any modification or replacement
#!/bin/bash
var="thress year"
myvar=$(cat <<'EOF'
today is friday
qqche
$var
EOF
)
echo $myvar

Two 、Expect
1、 Basic commands
Based on the tcl A tool based on language , It is often used for automatic control and testing , solve shell Exchange related problems in scripts
Install package
[root@localhost ~]# yum install expect -y
(1) Script interpreter
expect The script first introduces the file , Indicate which one is used shell
[root@localhost ~]# which expect
/usr/bin/expect
(2)spawn
spawn Usually followed by a Linux Carry out orders , Open a conversation 、 Start the process , And track subsequent interaction information . example :spawn passwd root
(3)expect
Judge whether the last output result contains the specified string , If so, return immediately , Otherwise, it will wait for the timeout to return
Can only be captured by spawn The output of the started process
Used to receive output after command execution , And then match the expected string
(4)send
Send string to process , The user simulates the user's input ; This command does not automatically enter to wrap lines , Generally, we need to add \r( enter ) perhaps \n
(5)interact
Keep interactive after execution , Give control to the console
(6)set
Set timeout , If it expires, continue to execute subsequent instructions
The unit is seconds
timeout -l Means never time out
By default ,timeout yes 10 second
(7)exp_continue
allow expect Continue down the instruction
analysis : If there is a statement error , No addition exp_continue, That's it , sign out
add exp_continue Will continue to execute subsequent statements , Do not exit .
(8)send_user
Echo command , amount to echo
(9) Receiving parameters
Expect Scripts can accept from bash Parameters passed
have access to [lindex $argv n] get
n from 0 Start , They are the first , the second , Third … Parameters
/bin/bash The location variable is from $1 Start to $9 end
Expect[lindex $argv 0] amount to /bin/bash Of $1
2、 example
Set up ssh Log in without interaction .
#!/usr/bin/expect
# ssh No interaction
set timeout 60 Set timeout 60 second
set hostname [lindex $argv 0] Set variable name , The value of the variable comes from the position variable 0
set password [lindex $argv 1]
log_file a.log Set the folder where log file records are saved
log_user 1 Record screen output
spawn ssh root@$hostname Initiate the startup process ssh A host name
expect {
"(yes/no)" {send "yes\r;exp_continue"}
"password" {send "$password\r"}
}
interact

版权声明
本文为[qq_ fifty-two million eight hundred and twenty-five thousand si]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230957317154.html
边栏推荐
猜你喜欢

2022年上海市安全员C证考试题库及答案

Yarn核心参数配置

Sim Api User Guide(5)

C language: expression evaluation (integer promotion, arithmetic conversion...)

【无标题】

《Redis设计与实现》

Juc并发编程09——Condition实现源码分析

Juc并发编程06——深入剖析队列同步器AQS源码

How to use SQL statement union to get another column of another table when the content of a column in a table is empty

2022年制冷与空调设备运行操作考试练习题及模拟考试
随机推荐
[educational codeforces round 80] problem solving Report
F-niu Mei's apple tree (diameter combined)
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——4视觉系统中的多故障
SQL tuning series - Introduction to SQL tuning
Chapter II in memory architecture (im-2.2)
Introduction to graph theory -- drawing
php 二维数组指定元素相等后相加否则新增
ansible 云计算 自动化
Realize data value through streaming data integration (3) - real-time continuous data collection
中控学习型红外遥控模块支持网络和串口控制
Computer network security experiment II DNS protocol vulnerability utilization experiment
Windows安装redis并将redis设置成服务开机自启
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
Classic routine: DP problem of a kind of string counting
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
Realize data value through streaming data integration (2)
正大国际讲解道琼斯工业指数到底是什么?
第三章 启用和调整IM列存储的大小(IM-3.1)