Non interactive operation of Shell script

1, Here Document interaction free

1.1 definition of interaction free

  • Provides a list of commands to interactive programs using I/O redirection

  • As an alternative to standard input, it can help script developers not use temporary files to build input information, but directly produce a file in place and use it as standard input for commands. Here Document can be used with non interactive programs and commands

1.2 syntax format

  1. Syntax format

  2. Command < < tag

  3. ....

  4. Input content

  5. ......

  6. sign

matters needing attention

  1. The tag can use any legal character (the common character is EOF)

  2. The mark at the end must be written in the top grid, and there must be no characters (including spaces) in front of it

  3. There must also be no characters (including spaces) after the tag at the end

  4. Spaces before and after the opening mark are omitted

  5. Single quotation mark variable double quotation mark -

1.3 examples

Example 1: use the wc -l command followed by the file name directly to count the number of lines of content in the file, place the content to be counted between the tags "EOF", and directly transfer the content to wc -l for statistics.

[root@localhost ~]#wc -l <<EOF
> 1
> 2
> 3
> EOF
3

Example 2: there is usually an interactive process when using the read command to receive the user's input value. Variable values can be entered between two EOF tags

##The read command has an interactive process
[root@localhost ~]# read -p "please enter a number" ack
 Please enter a number 8
[root@localhost ~]# echo $ack 
8
 
###If interaction free is used, the value between EOF s will become the parameter of variable i
[root@localhost ~]# read i <<EOF
> hello
> EOF
[root@localhost ~]#echo $i
hello

Example 3: setting the password using the passwd command

[root@localhost ~]#passwd yxp <<EOF
> 123123
> 123123
> EOF

1.4Here Document variable setting

Here Document also supports the use of variables. If variables are used between tags, the variable value will be replaced first. If you want to write some content to a file, in addition to the conventional method, you can also use Here Document. If the written content contains variables, the variables should be replaced with the actual values before writing the file, and the writing should be completed in combination with the cat command.

Example 1: when writing a file, the variable will be replaced with the actual value, and then the writing will be completed in combination with the cat command

[root@localhost data]#vim eof1.sh 
 
#!/bin/bash
file="yxp.txt"
var="park"
cat <<EOF >$file
I am going to the $var
EOF

Example 2: assign an overall value to the variable output, and then print the variable value through the echo command. It would be better to call the variable with double quotation marks

[root@localhost data]#vim eof1.sh
#!/bin/bash
file="yxp1.txt"
var="park"
my=$(cat <<EOF >$file
I am going to the $var
EOF
)
echo $my

Example 3: add double quotation marks on the tag to turn off the function of variable replacement

1.6 multiline notes

Bash's default annotation is "#". This annotation method only supports single line annotation: the introduction of Here Document solves the problem of multi line annotation

":" represents an empty command that does nothing. The contents of the middle mark area will not be executed and will be ignored by bash, so it can achieve the effect of batch annotation

#!/bin/bash
file="yxp2.txt"
var="park"
myvar=$(cat <<EOF >$file
I am going to the $var.
I will go to play with my friends.
I am very happy.
 
EOF
)
echo $myvar
###The following part is annotated and will not be displayed
:<<EOF
echo "I am going to the $var"
echo "I am very happy."
EOF

2, expect

2.1 expect definition

It is a tool based on tcl (tool command language). It is often used for automatic control and testing to solve the problems related to interaction in shell scripts

2.2 expect installation

rpm -q expect
rpm -q tcl
yum install -y expect

2.3 related commands in expect

script interpreter

The expect script first introduces a file to indicate what kind of shell is used

    #!/usr/bin/expect

spawn starts a new process (monitor, capture)

Spawn is usually followed by a Linux execution command, which means to start a session, start a process, and track the subsequent interaction information, for example: spawn passwd root

expect receives a string from the process

Judge whether the last output result contains the specified string. If so, it will be returned immediately. Otherwise, it will be returned after the timeout time;

Only the output of the process started by spawn can be captured;

It is used to receive the output after the command is executed, and then match the expected string

Send is used to send a string to the process

Send a string to the process to simulate the user's input;

This command cannot automatically enter and line feed. Generally, it needs to add \ r (enter) or \ n

exp_continue matches multiple strings and adds this command after executing the action

    exp_ continue is similar to the continue statement in the control statement. Indicates that expect is allowed to continue to execute instructions downward

expect eof

Indicates the end of interaction, waits for the end of execution, and returns to the original user, corresponding to spawn

For example, when switching to the root user, the expect script waits for 10s by default. After executing the command, it stays for 10s by default and automatically switches back to the original user

Interact allows users to interact

It will stay at the target terminal instead of returning to the original terminal. At this time, it can be operated manually. Life after interact ion Make it ineffective;

For example, adding exit after interact does not exit the root user. If there is no interaction, it will exit after login, rather than stay on the remote terminal.

Using interact will remain at the terminal without returning to the original terminal;

set

The default timeout of expect is 10 seconds. The session timeout can be set through the set command. If the timeout is not limited, it should be set to - 1

Example: set time out 30

 send_users

Indicates that the echo command is the same as echo

Receive parameters

The expect script can accept passing parameters from the bash command line and obtain them using [lindex $argv n]. Where you start from 0, which means the first, second and third parameter

Example:

[hostname = $0] is equivalent to $hostname=$

set password [lindex $argv 1] is equivalent to passswd=

set hostname [lindex $argv 0] is equivalent to hostname=

set password [lindex $argv 1] is equivalent to passswd=
  

2.4 examples

2.4.1 interactive su switching free users

[root@localhost opt]#vim expect.sh 
 
#!/usr/bin/expect
 
#Set timeout
set timeout 5
set hostname [lindex $argv 0]
#hostname=$1
set password [lindex $argv 1]
#password=$2
 
#Start tracking command
spawn su $hostname
expect "password:" {send "123123\n"}
#No interactive execution, capture information and match
expect "]#"
send_user "hello"
 
 
#Give control to the console
interact
#expect eof

2.4.2 interactive free remote ssh script

[root@localhost opt]#vim ssh.sh 
 
#!/usr/bin/expect
#The ssh Remote control command should be followed by the ssh Remote control command
spawn ssh 192.168.59.105
expect {
        "yes/no" { send "yes\n";exp_continue }
        "password" { send "123123\n"}
 
}
interact

2.4.3 interaction free scp

[root@localhost opt]#vim scp.sh 
 
#!/usr/bin/expect
spawn scp /etc/passwd 192.168.59.105:/data
expect {
   "yes/no" { send "yes\n";exp_continue }
   "password: " { send "123123\n" }
}
expect eof

Keywords: linq

Added by maexus on Mon, 14 Feb 2022 16:06:00 +0200