当前位置:网站首页>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
边栏推荐
- Failureforwardurl and failureurl
- [untitled]
- ansible 云计算 自动化 命令行精简版
- 2022年流动式起重机司机考试题库模拟考试平台操作
- P1446 [hnoi2008] cards (Burnside theorem + DP count)
- SQL tuning series - Introduction to SQL tuning
- [lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
- Number theory blocking (integer division blocking)
- DBA常用SQL语句(6)- 日常管理
- 2022茶艺师(初级)考试试题模拟考试平台操作
猜你喜欢
![[educational codeforces round 80] problem solving Report](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[educational codeforces round 80] problem solving Report

101. Symmetric Tree

Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview

Depth selector

【无标题】

一文看懂 LSTM(Long Short-Term Memory)

杰理之更准确地确定异常地址【篇】

Educational Codeforces Round 81 (Rated for Div. 2)
![[untitled]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[untitled]

NEC infrared remote control coding description
随机推荐
实践六 Windows操作系统安全攻防
2022年制冷与空调设备运行操作考试练习题及模拟考试
Prefix sum of integral function -- Du Jiao sieve
C language: expression evaluation (integer promotion, arithmetic conversion...)
DBA常用SQL语句(4)- Top SQL
最长公共前串
A concise course of fast Fourier transform FFT
LeetCode-608. 树节点
2022年流动式起重机司机考试题库模拟考试平台操作
NEC infrared remote control coding description
通过流式数据集成实现数据价值(1)
DBA common SQL statements (3) - cache, undo, index and wait events
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
防疫登记小程序
ansible 云计算 自动化
Educational Codeforces Round 81 (Rated for Div. 2)
[untitled]
Classic routine: DP problem of a kind of string counting
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
SQL调优系列文章之—SQL性能方法论