Shell programming without interaction

1. Overview of Here Document

Use I/O redirection to provide a command list to an interactive program or command, such as the ftp, cat, or read commands.
It is a substitute for standard input that can help script developers not have to use temporary files to build input information, but directly produce a “file” in place and use it as the standard input of “commands”. Here Document can also be used with non-interactive programs and commands.

2. Basic grammar format

command <<tag
...
Content # between the tags is the incoming content
...
tag

Notes:
Mark can use any legal character (usually EOF)
The end mark must be written in the top case, and there cannot be any characters in front of it
There must also not be any characters (including spaces) after the closing tag
Spaces before and after the opening tag are omitted

1, the command line realizes interaction-free

2, Here Document free interaction

3, variable setting

1) Variable substitution

#!/bin/bash
file="ky28.txt"
i="beizhai"
cat > $file <<EOF
welcome to class $i
EOF
~

2) Turn off variable substitution function

4, remove the TAB character before each line

Add ‘-‘ in front of the mark on the first line, this means to suppress the function of TAB at the beginning of each line

5, multi-line comment

Bash’s default comment is “#”, this comment method only supports single-line comments, in the work of Shell script, any character string on the right side of “#”, bash will ignore it.

:” represents an empty command that does nothing. The content in the middle marked area will not be executed and will be ignored by bash, so the effect of batch comments can be achieved.

Third, expect realizes interaction-free

1, Introduction to expect tool

Expect is a tool based on the tcl language. It is often used for automated control and testing, and to solve interaction-related problems in shell scripts.

Two packages need to be installed: expect, tcl. But after installing expect, there will be tcl (yum will install tcl after resolving dependencies)

2, install expect

Check the expect installation package:
rpm -q expect

Check the dependency package tcl:

rpm -q tcl

Install and download the expect tool

yum install -y expect #install expect (tcl package will be installed as a dependency)

3, the basic usage of expect

1) Declare the script interpreter

2) spawn starts the process and captures interaction information

spawn is usually followed by a Linux execution command, which means opening a session, starting a process, and tracking subsequent interaction information.

3) expect receives a string from the process

  • Judge whether the last output result contains the specified string, if so, return immediately, otherwise wait for the timeout and return;
  • Only captures output from processes started by spawn;
  • It is used to receive the output after the command is executed, and then match it with the expected string.

4) send is used to send a string to the process

  • Send a string to the process to simulate user input;
  • This command cannot automatically return and line feed, generally add \r (carriage return) or \

5) End character (expect eof and interact can only choose one of two)

expect eof

  • Indicates that the interaction is over, wait for the execution to end, and return to the original user, corresponding to spawn.
  • For example, if you switch to the root user, the expect script will wait for 10s by default. After the command is executed, it will stay for 10s by default and then automatically switch back to the original user

interact

  • Keep the interactive state after the execution, and the control will stay in the target terminal instead of returning to the original terminal when the control is handed over to the console. At this time, it can be operated manually, and the command after the interact will not work;
  • Using interact will remain in the terminal and will not return to the original terminal;

6) set set session timeout

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

7) exp_continue

exp_continue is appended to an expect judgment item, so that after the item is matched, it can continue to match other items in the expect judgment statement.
exp_continue is similar to the continue statement in control statements. Indicates that expect is allowed to continue executing instructions downward.
Execute in parallel, if there is a match before exp_continue, it will be executed, if not, it will not be executed; but the latter will definitely be executed.

8) send_users

Indicates the echo command, which is equivalent to echo

9) Receive parameters

The expect script can accept arguments passed from the bash command line, obtained using [lindex $argv n]. Among them, you start from 0, which means the first, second, third…..parameters

4, expect interactive script application

1) Interaction-free default value to modify user password

2) Pass in the obtained value through the location variable, and modify the user password

3) ssh remote login

4) fdisk partition operation