File: redirects file descriptor 3 to file. After the standard output is redirected to 3, the standard output content is written to file. Similarly, when reading from the standard input, the standard input is directedTo 3, the file content is printed. The preceding result shows that exec 3 <> file is only valid for one command.
N> file, open the file in writing, and redirect n to the file.For example:
Root @ gmdz-virtual-machine :~ /Test # exec 3> file
Root @ gmdz-virtual-machine :~ /Test # ls> & 3
Root @ gmdz-virtual-machine :~ /Test # cat file
A
B
Root @ gmdz-virtual-machine :~ /Test # cat <& 3
Cat:-: incorrect file descriptor
N <file, open the file in read format, and redirect n to the file.For example:
Root @ virtual-machine :~ /Test # cat file
Test
Root @ virtual-machine :~ /Test # exec 3 <file
Root @ virtual-machine :~ /Test # ls> & 3
Ls: write error: incorrect file descriptor
Root @ virtual-machine :~ /Test # cat <& 3
Test
Root @ virtual-machine :~ /Test # cat <& 3
Root @ virtual-machine :~ /Test #
Similarly, exec 3 <file is only valid for one command.
<Label: here-document<Label redirection forces the input of a command to use the standard input of shell, and reads only lines containing label. The input is called here-document.
For example:
# Cat> msgfile <EOF
> 1
> 2
> 3
> EOF
Root @ gmdz-virtual-machine :~ /Test # cat msgfile
1
2
3
Redirection
Root @ virtual-machine :~ /Test # cat <EOF
> $ PWD
> EOF
/Root/test
Root @ virtual-machine :~ /Test # cat <"EOF"
> $ PWD
> EOF
$ PWD
The second variation is <-, which deletes the leading TAB from the here-document and label lines (but does not delete spaces ).
For example, the script content is as follows:
#! /Bin/bash
Cat <-EOF
123
456
EOFAfter execution, the output is:
123
456
N> file: redirects the file descriptor n to the file. If the file exists, it is appended to the back end. For example, command 2> filen> &: Copies the standard output to the file descriptor n.
N <&: Copy standard input from file descriptor n
N> & m: Make file descriptor n a copy of output file descriptor m.For example:
Root @ virtual-machine :~ /Test # cat file
Test
Root @ virtual-machine :~ /Test # ls nosuchfile
Ls: unable to access nosuchfile: No file or directory
Root @ virtual-machine :~ /Test # ls nosuchfile> file 2> & 1
Root @ virtual-machine :~ /Test # cat file
Ls: unable to access nosuchfile: No file or directory
N <& m: Make file descriptor n a copy of input file descriptor m &> file: export standard output and error output to file.For example:
Root @ virtual-machine :~ /Test # ll a nosuchfile &> file
Root @ virtual-machine :~ /Test # cat file
Ls: unable to access nosuchfile: No file or directory
-Rw-r -- 1 root 0 2014-08-01 09:09
<&-: Disable Standard input.For example, the script is as follows:
Exec <&-
Cat
Execution result:
#./A. sh
Cat:-: incorrect file descriptor
Cat: Disable Standard Input: incorrect file descriptor
> &-: Disable Standard outputFor example, the script is as follows:
Exec> &-
Echo "test"
Execution result:
#./A. sh
./A. sh: 2nd rows: echo: write error: incorrect file descriptor
N> &-: Disable output from file descriptor nFor example:
Root @ gmdz-virtual-machine :~ # Ls
A. sh file test
Root @ virtual-machine :~ # Exec 3 <> file
Root @ virtual-machine :~ # Ls> & 3
Root @ virtual-machine :~ # Exec 3 <> file
Root @ virtual-machine :~ # Exec 3> &-
Root @ virtual-machine :~ # Ls> & 3
-Bash: 3: incorrect file descriptor
N <&-: Disable input from file descriptor nFor example:
Root @ gmdz-virtual-machine :~ # Exec 3 <> file
Root @ gmdz-virtual-machine :~ # Cat <& 3
A. sh
File
Test
Root @ virtual-machine :~ # Exec 3 <> file
Root @ virtual-machine :~ # Exec 3 <&-
Root @ virtual-machine :~ # Cat <& 3
-Bash: 3: incorrect file descriptor
2. String I/O2.1.echoEcho simply prints the parameters to the standard output. The following table lists the options for echo to receive dashes.
-E: Explanation of escape characters with diagonal lines
-E: Disable the interpretation of escape characters on the backslash of the subsystem. This is the default mode.
-N: the last line feed is omitted.
For example:
Root @ virtual-machine :~ # Echo-e "a \ ta"
A
Root @ virtual-machine :~ # Echo-E "a \ ta"
A \ ta
Root @ virtual-machine :~ # Echo-n "a \ ta"
A \ taroot @ virtual-machine :~ #
2.2.readRead allows you to read values to shell variables. The basic syntax is:
Read var1 var2...
This statement receives a line from the standard input and forms words separately. Words are separated by any character in the value of the environment variable IFS. The word is assigned to the variable.
Var1, var2...
For example, the content of a. sh is:
Read v1 v2
Echo $ v1
Echo $ v2
Execution result:
#./A. sh
123 456
123
456
If there are more words than variables, the additional words are assigned to the final variables. If you omit the variables, the entire input is assigned to the REPLY variable.
For example, execute the above script again:
#./A. sh
1 2 3 4
1
2 3 4
Modify a. sh:
Read
Echo $ REPLY
The execution result is:
#./A. sh
1 2 3 4
1 2 3 4
The following describes how to read variables from a file.
For example, the file content is as follows:
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2
Method 1:
Use the following a. sh script to read the variables:
While read v1 v2; do
Echo $ v1 $ v2
Done
Execution result:
#./A. sh <file
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2
Method 2:
Modify a. sh:
While read v1 v2; do
Echo $ v1 $ v2
Done <file
Execution result:
#./A. sh
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2
Method 3:
Modify a. sh:
Func (){
While read v1 v2; do
Echo $ v1 $ v2
Done
}
Func <fileExecution result:
#./A. sh
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2
Method 4:
Modify a. sh:
{
While read v1 v2; do
Echo $ v1 $ v2
Done
} <FileExecution result:
#./A. sh
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2
Read has three common options:-a,-p, and-r.
-A allows you to read values into an array. Each continuous entry is assigned to a given array starting with 0. For example, the content of a. sh is as follows:
Read-a people
Echo $ {people [0] }$ {people [1] }$ {people [2]}
Execution result:
#./A. sh
1 2 3
1 2 3
-P option prints the string before reading the input. For example:
[Root @ yanPC ~] # Read-p "dir? "Dirname
Dir? /Home
[Root @ yanPC ~] # Echo $ dirname
/Home
The-r option does not ignore the "\" character when reading the input.
If "\" is inside the input string:
[Root @ yanPC ~] # Read dirname
123/456
[Root @ yanPC ~] # Echo $ dirname
123456
[Root @ yanPC ~] # Read-r dirname
123/456
[Root @ yanPC ~] # Echo $ dirname
123/456
If "\" is at the end of the input string:
[Root @ yanPC ~] # Read dirname
123 \
> 456
[Root @ yanPC ~] # Echo $ dirname
123456
[Root @ yanPC ~] # Read-r dirname
123 \
[Root @ yanPC ~] # Echo $ dirname
123 \