Linux and linux

Source: Internet
Author: User

Linux and linux
1. Prerequisites

Standard input (stdin), standard output (stdout), and standard error (stderr) are often used when writing scripts, because the output information may be one of the above.

A file descriptor is an integer associated with an opened file or data stream. File descriptors 0, 1, and 2 are reserved by the system.

  • 0-stdin (standard input)
  • 1-stdout (standard output)
  • 2-stderr (standard error)
2. Instance
echo This is a sample. > temp.txt

Output texttemp.txtThe file is truncated, that is, the content in the file is cleared before the echo command is written.

echo This is a sample. >> temp.txt

The above command is an append method.

Before redirection, let's take a look at the successful and unsuccessful commands.

When a command fails and exits, it returns a non-zero value. 0 is returned after the operation is successful. You can use$?View the returned value.

ls 2> out.txt

Output the error to the file.

You can also:

cmd 2> out.txt 1> temp.txt

Redirects standard errors and standard output to two files. Of course, there is also a more streamlined way to output to the same file:

cmd 2>&1 out.txt

Sometimes you don't want to save the error output, you can direct it to a "black hole "(/dev/null.

What if you want to save the output and view the output?

command | tee out.txt | cat -n

In this case, the command result is saved inout.txtIn the media transcoding queue.cat -nAdd a row number to the result.

We can also usestdinAs a command parameter. You only need to use-as the command file name.

$ echo who is this | tee -who is thiseho is this
3. Principles

The default redirection operator uses standard output. Therefore>Equivalent1>.

4. Supplement

SlavestdinThe read command can receive data in multiple ways.

Redirects a file to a command. With redirection, we can usestdinIn this way, data is read from the file.

$ cmd < file

Custom file descriptor:

Create a file descriptor to read the file:

$ Exec 3 <input.txt # use file descriptor 3 to open and read files

We can use it like this:

$ echo this is a test > input.txt$ exec 3<input.txt

In this case, file descriptor 3 can be used in the command.

$ cat <&3this is a test

If you want to read it again, you cannot use 3. You need to allocate the read again.

Create a file descriptor for writing:

$ exec 4>output.txt$ echo newline > &4$ cat outlinenewline

Dev/stdin-> proc/self/fd/0-> dev/pts/1

The input and output in the standard operation point to the terminal of the current process in the virtual directory.

1.2 redirection & pipeline principle: Redirection
$ tail -f /var/log/message  > abc

And then hangs.psCheck whether the process ID is15551.

$ cat /proc/15551/fd/?

You can see:

/Proc/15551/fd/0->/dev/pts/1/proc/15551/fd/1->/roor/test/abc # redirect output file of the preceding command/ proc/15551/fd/2->/dev/pts/1/proc/15551/fd/3->/var/log/message/proc/15551/fd/4-> inotify

Redirection is to direct the link to the executed file.

MPs queue

Use the standard output of the preceding command as the standard input for subsequent commands.

Note:Standard outputThat is to say, the standard error output has been filtered out during pipeline transfer. Therefore, the standard error output of the preceding command is printed on the terminal.

Many Commands do not directly support standard input.

$ which find | ls -l

Cannot outputfind. Use the following method.

$ which find | xargs ls -l 

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.