Redirection standard output
> Redirects the standard output (STDOUT) of the current command to a file or descriptor.
The following example stores the lscommand output to the File.txt file.
Ls> file.txt
> File.txt ls
If the target file does not exist, it will be created or truncated.
If this parameter is not specified, the default redirect descriptor is standard output or 1. The following command is equivalent to the preceding example:
Ls 1> file.txt
Append vs truncation
Truncation>
1. If the file does not exist, create
2. Truncation (delete file content)
3. Write files
$ Echo "first line">/tmp/lines
$ Echo "second line">/tmp/lines
$ Cat/tmp/lines
Second line
Append>
1. If the file does not exist, create
2. Append the file (write at the bottom of the file)
# Overwrite existing file
$ Echo "first line">/tmp/lines
# Append a second line
$ Echo "second line">/tmp/lines
$ Cat/tmp/lines
First line
Second line
Redirection standard output and standard errors
File descriptors 0 and 1 are pointers. We changed the object descriptor.
>/Dev/null indicates that 1 points to/dev/null.
First, point 1 (STDOUT) to/dev/null, and then 2 to 1 (no matter what 1 points ).
Echo_to_stdout_and_stderr>/dev/null 2> & 1
It can be shorter:
Echo_to_stdout_and_stderr &>/dev/null
Use named pipe
Sometimes you want to use the standard output of a program as the standard input of multiple other programs, you cannot use the standard pipeline at this time, but you can write a temporary file, such:
Touch tempFile.txt
Ls-l> tempFile.txt &
Grep ". log" <tempFile.txt
This token can be used in different situations, but no one knows which program the tempfile.txt will be deleted or modified. At this time, the named pipeline can be used.
Mkfifo myPipe
Ls-l> myPipe
Grep ". log" <myPipe
MyPipe is technically a file, so we can use ls-l to check the Directory of the currently created MPs queue.
Mkdir pipeFolder
Cd pipeFolder
Mkfifo myPipe
Ls-l
Output:
Prw-r-1 root 0 Jul 25 :20 myPipe
Note that the first character of the permission is pipe, not a file.
Now we are doing something interesting.
Open a terminal and create an MPs queue in an empty directory:
Mkfifo myPipe
Now let's enter something to the pipeline:
Echo "Hello from the other side"> myPipe
You will notice that this command has been suspended. Let's open a new terminal and enter:
Cat <myPipe
When "hello from the other side" is output, Terminal 1 is complete, and Terminal 2 is the same.
Now we run the program in reverse direction, execute cat <myPipe first, and then input something to myPipe. It still works as expected, because a program will wait until something is entered in the pipeline. Naming pipelines are useful when information is transmitted between terminals or programs.
Output error information to standard error
The error message is usually included in the script for scheduling. A simple output error message is as follows:
Cmd | echo 'command failed'
It may work in simple scenarios, but it is not the usual practice. In this example, the error message will pollute the actual output of the script. In short, error messages should be output to standard errors rather than standard output, such:
Cmd | echo 'command failed'>/dev/stderr
Other examples:
If cmd; then
Echo 'success'
Else
Echo 'command failed'>/dev/stderr
Fi
It can be encapsulated into a function:
Err (){
Echo "E: $ *" >>/ dev/stderr
}
Err "My error message"