当前位置:网站首页>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
边栏推荐
- MapReduce核心和基础Demo
- [educational codeforces round 80] problem solving Report
- Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
- P1390 sum of common divisor (Mobius inversion)
- 代码源每日一题 div1 (701-707)
- SQL tuning series - SQL performance methodology
- DBA common SQL statements (1) - overview information
- Sim Api User Guide(4)
- DBA common SQL statements (3) - cache, undo, index and wait events
- Code source daily question div1 (701-707)
猜你喜欢
《谷雨系列》空投
"Gu Yu series" airdrop
Career planning and implementation in the era of meta universe
failureForwardUrl与failureUrl
NEC infrared remote control coding description
Easy to understand subset DP
Yarn核心参数配置
Example of data object mask used by SAP translate
Comparative analysis of meta universe from the dimension of knowledge dissemination
Juc并发编程06——深入剖析队列同步器AQS源码
随机推荐
一文读懂PlatoFarm新经济模型以及生态进展
shell脚本免交互
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
Prefix sum of integral function -- Du Jiao sieve
Windows安装redis并将redis设置成服务开机自启
NEC infrared remote control coding description
构建元宇宙时代敏捷制造的九种能力
[educational codeforces round 80] problem solving Report
DBA常用SQL语句(2)— SGA和PGA
自定义登录失败处理
《Redis设计与实现》
杰理之通常影响CPU性能测试结果的因素有:【篇】
深度选择器
Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
ansible 云计算 自动化
【无标题】
Realize data value through streaming data integration (3) - real-time continuous data collection
杰理之更准确地确定异常地址【篇】
使用IDEA开发Spark程序