Learning bash notes-Input and Output

Source: Internet
Author: User
1. I/O redirection

The I/O redirection is as follows:

  • Cmd1 | cmd2: pipeline that receives the standard output of cmd1 as the standard input of cmd2.
  • > File: redirects standard output to file.
  • <File: receives standard input from File
  • > File: redirects the standard output to file. If the file exists, it is appended
  • > | File: Even if noclobber is set, the standard output is still forced to file.

Shell provides a feature called noclobber, which prevents the existing files from being overwritten inadvertently during redirection. By setting the noclobber variable, you can

Open this feature. If you redirect the output to an existing file, shell reports the error message and does not execute the redirection command.

For example:

[Email protected]: ~ # Cat a.txt
123
[Email protected]: ~ # Set-o | grep "noclobber"
Noclobber off
[Email protected]: ~ # Echo "test1"> a.txt
[Email protected]: ~ # Cat a.txt
Test1
[Email protected]: ~ # Set-O noclobber
[Email protected]: ~ # Set-o | grep "noclobber"
Noclobber on
[Email protected]: ~ # Echo "hello"> a.txt
-Bash: a.txt: The existing files cannot be overwritten.

[Email protected]: ~ # Echo "hello"> | a.txt
[Email protected]: ~ # Cat a.txt
Hello

  • N> | file: The file is forcibly output from file descriptor n even if noclobber is set.

For example:

[Email protected]: ~ # Cat a.txt
123
[Email protected]: ~ # Set-o | grep "noclobber"
Noclobber off
[Email protected]: ~ # Ls nosuchfile 2> a.txt
[Email protected]: ~ # Cat a.txt
Ls: unable to access nosuchfile: No file or directory
[Email protected]: ~ # Echo "123"> a.txt
[Email protected]: ~ # Cat a.txt
123
[Email protected]: ~ # Set-O noclobber
[Email protected]: ~ # Set-o | grep "noclobber"
Noclobber on
[Email protected]: ~ # Ls nosuchfile 2> a.txt
-Bash: a.txt: The existing files cannot be overwritten.
[Email protected]: ~ # Cat a.txt
123
[Email protected]: ~ # Ls nosuchfile 2> | a.txt
[Email protected]: ~ # Cat a.txt
Ls: unable to access nosuchfile: No file or directory

  • <> File: use file as both input and output.
It is mainly used for device files, that is, hardware devices that correspond to terminals and communication lines. Underlying system programmers can use it to test the device drive. Otherwise, it is not very useful.
  • N <> file: open the file in read/write mode, and redirect n to the file.

To describe the redirection, first describe the exec command:

Shell's built-in command exec will not start a new shell, but will replace the current shell process with the command to be executed, and the environment of the old process will be cleared, and the exec command

Other commands will not be executed. Therefore, if you execute exec ls in a shell, the shell will exit after the current directory is listed.

The shell process has been replaced by a process that only executes the LS command. After the execution ends, the shell process exits. However, pay attention to one exception. When the exec command operates on the file descriptor

The shell will not be replaced, and the subsequent commands will be executed after the operation is complete.

  1. $ Exec 3 <> File
  2. $ Ls> & 3
  3. $ Cat File
  4. File
  5. $ Cat <& 3
  6. $ Exec 3 <> File
  7. $ Cat <& 3
  8. File
After the file descriptor 3 is directed to file and the standard output is directed to 3, the standard output content is written to file. Similarly, when reading from the standard input, the standard input is directed

To 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:

[Email protected]: ~ /Test # exec 3> File
[Email protected]: ~ /Test # ls> & 3
[Email protected]: ~ /Test # Cat File
A
B
[Email protected]: ~ /Test # Cat <& 3
Cat:-: incorrect file descriptor

  • N <file, open the file in read format, and redirect n to the file.

For example:

[Email protected]: ~ /Test # Cat File
Test
[Email protected]: ~ /Test # exec 3 <File
[Email protected]: ~ /Test # ls> & 3
Ls: write error: incorrect file descriptor
[Email protected]: ~ /Test # Cat <& 3
Test
[Email protected]: ~ /Test # Cat <& 3

[Email protected]: ~ /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
[Email protected]: ~ /Test # Cat msgfile
1
2
3

Redirection

[Email protected]: ~ /Test # Cat <EOF
> $ Pwd
> EOF
/Root/test
[Email protected]: ~ /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
EOF

After 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 end. For example, command 2> File
  • N> &: copy 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:

[Email protected]: ~ /Test # Cat File
Test
[Email protected]: ~ /Test # ls nosuchfile
Ls: unable to access nosuchfile: No file or directory
[Email protected]: ~ /Test # ls nosuchfile> file 2> & 1
[Email protected]: ~ /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: Specify standard output and error output to file.

For example:

[Email protected]: ~ /Test # ll a nosuchfile &> File
[Email protected]: ~ /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 output

For 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 n

For example:

[Email protected]: ~ # Ls
A. Sh file test

[Email protected]: ~ # Exec 3 <> File
[Email protected]: ~ # Ls> & 3

[Email protected] virtual-machine :~ # Exec 3 <> File
[Email protected]: ~ # Exec 3> &-
[Email protected]: ~ # Ls> & 3
-Bash: 3: incorrect file descriptor

  • N <&-: Disable input from file descriptor n

For example:

[Email protected]: ~ # Exec 3 <> File
[Email protected]: ~ # Cat <& 3
A. Sh
File
Test
[Email protected]: ~ # Exec 3 <> File
[Email protected]: ~ # Exec 3 <&-
[Email protected]: ~ # Cat <& 3
-Bash: 3: incorrect file descriptor


2. String I/o2.1.echo

Echo 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:

[Email protected]: ~ # Echo-e "A \ ta"
A
[Email protected]: ~ # Echo-e "A \ ta"
A \ Ta
[Email protected]: ~ # Echo-n "A \ ta"
A \ [email protected]: ~ #


2.2.read

Read 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 <File

Execution 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
} <File

Execution 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:

[[Email protected] ~] # Read-P "dir? "Dirname
Dir? /Home
[[Email protected] ~] # Echo $ dirname
/Home

The-r option does not ignore the "\" character when reading the input.

If "\" is inside the input string:

[[Email protected] ~] # Read dirname
123/456
[[Email protected] ~] # Echo $ dirname
123456
[[Email protected] ~] # Read-r dirname
123/456
[[Email protected] ~] # Echo $ dirname
123/456

If "\" is at the end of the input string:

[[Email protected] ~] # Read dirname
123 \
> 456
[[Email protected] ~] # Echo $ dirname
123456
[[Email protected] ~] # Read-r dirname
123 \
[[Email protected] ~] # Echo $ dirname
123 \

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.