Chapter 15 Presenting Data

insert image description here
By default, most bash commands will direct output to the STDOUT file descriptor.
insert image description here
Appending data to a file is done with the >> symbol.
insert image description here
whoThe output generated by the command will be appended to the existing data in the test2 file.

Only redirected errors STDERR file descriptor is set to 2. You can choose to redirect only error messages by placing the file descriptor value before the redirection symbol. The value must be placed immediately before the redirection symbol. In this way, the shell redirects only error messages, not normal data.
insert image description here
Since this command redirects the output of file descriptor 2 (STDERR) to an output file, the shell sends any error messages it generates directly to the specified redirected file.

If you want to redirect error and normal output , you must use two redirection symbols. You need to put the file descriptor corresponding to the data to be redirected in front of the symbol, and then point to the output file used to save the data.
insert image description here
The shell uses 1>symbols to redirect the normal output of the ls command to the test7 file, which should go to STDOUT
. All error messages that should be output to STDERR 2>are symbolically redirected to the test6 file.

&>Redirect the output of STDERR and STDOUT to the same output file with special redirection symbols .
insert image description here
The bash shell automatically gives higher priority to error messages than standard output.

temporary redirect

Use the output redirector to redirect output to the STDERR file descriptor. When redirecting to a file descriptor, you
must precede the file descriptor number with a &:
insert image description here

This line will display the text at the location pointed to by the script's STDERR file descriptor.
insert image description here
insert image description here
insert image description here

Redirect input in script

The exec command allows you to redirect STDIN to a file on Linux systems:

exec  0<  testfile

Take input from the file testfile, not STDIN. This redirection works whenever the script requires input.
insert image description here
This script redirects file descriptor 3 to another file with the exec command. When the script executes the echo statement, the output
is displayed on STDOUT as expected. But the output of the echo statement you redirected to file descriptor 3 went into
another file. This way you can keep normal output on the display, while redirecting specific information to a file (such as a
log file).

Instead of creating a new file, you can use the exec command to append the output to an existing file.
exec 3>>test13out
output will be appended to the test13out file instead of creating a new one.

A common way to temporarily redirect output and then restore default output settings.
insert image description here
insert image description here
First, the script redirects file descriptor 3 to the current location of file descriptor 1, which is STDOUT. This means that any output sent to file descriptor 3 will appear on the display. The second exec command redirects STDOUT to a file, and the shell will now redirect output sent to STDOUT directly into the output file. However, file descriptor 3 still points to where STDOUT used to be, which is the monitor. If you send output data to file descriptor 3 at this point, it will still appear on the display, even though STDOUT has been redirected.
After sending some output to STDOUT (now pointing to a file), the script redirects STDOUT to the
current location of file descriptor 3 (now still the display). This means that STDOUT now points to where it used to be: the monitor .

Create input file descriptor

exec 6<&0
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
Done
exec 0<&6
read -p "Are you done now? " answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "Sorry, this is the end.";;
esac

File descriptor 6 is used to hold the location of STDIN. The script then redirects STDIN to a file.
All input to the read command comes from the redirected STDIN (that is, the input file).
After reading all the lines, the script redirects STDIN to file descriptor 6, thus restoring STDIN to its original
location. Use another read command to test whether STDIN is back to normal. This time it waits for keyboard input.

The standard location for the null file on a Linux system is /dev/null. Any data you redirect to that location will be discarded and
not displayed.
A common method for clearing log files, since log files must always be ready for application operations.
insert image description here
The Linux system has a special directory for temporary files.
Linux uses the /tmp directory to store files that don't need to be kept permanently . Most Linux distributions are configured to automatically delete all files in the /tmp directory when the system starts.
Any user account on the system has permission to read and write files in the /tmp directory. This feature gives you an
easy way to create temporary files without having to worry about cleanup.
The mktemp command creates a unique temporary file in the /tmp directory.

Send output to both the display and the log file. Just use the special tee command.
The tee command is equivalent to a T-joint for pipes. It sends data from STDIN to two places at the same time. One is
STDOUT, the other is the file name specified by the tee command line
insert image description here

You can use it with pipeline commands to redirect command output.
insert image description here
Review the pipeline commands here.

pipeline command

In the command line environment, the pipeline command (Pipeline) is a very useful technology, which allows multiple commands to be connected, and the output of one command is used as the input of another command. In this way, the transmission and processing of data streams can be realized, and the flexibility and efficiency of the command line can be improved.

In most Unix-like systems (such as Linux, macOS), pipe commands |are represented by the vertical bar symbol. Here are some examples of common pipeline commands:

  1. Simple pipeline:

    command1 | command2
    

    This command takes the output command1of as input to command2. command1After execution, its output is passed to command2for processing.

  2. Filter and sort:

    cat file.txt | grep "keyword" | sort
    

    This command file.txtpasses the contents of the file to grepthe command for keyword filtering, and then passes the filtered results to sortthe command for sorting.

  3. Statistics command output:

    ls -l | wc -l
    

    This command ls -lpasses the output of the command to wc -lthe command for line counting to get the number of files or directories.

  4. Multiple pipelines:

    command1 | command2 | command3
    

    This command command1pipes the output of to command2, which in turn command2pipes the output of to command3. Multiple commands can be concatenated via multiple pipe commands.

By using pipeline commands, multiple simple commands can be combined to achieve more complex data processing and manipulation. Pipeline commands are very flexible and powerful in the command line environment, which can greatly improve the efficiency and convenience of the command line.

Guess you like

Origin blog.csdn.net/qq_44710568/article/details/131915597