Linux Basic Management-"standard IO and redirects and pipelines"

Source: Internet
Author: User
Tags stdin

1. Standard input and output


In general, the program is composed of instruction + data, so it is unavoidable to read and output data, that is, input and outputs. Open files have a Fd:file descriptor (file descriptor), you can see in/proc/$$/fd the number of files opened by the current terminal, a file descriptor is an open file.

[[Email protected] ~] #ll/proc/$$/fdtotal 0lrwx------. 1 root root 12:27 0,/dev/pts/5lrwx------. 1 root root 12:27 1,/dev/pts/5lrwx------. 1 root root 12:27 2,/dev/pts/5lrwx------. 1 root root 15:20 255-/DEV/PTS/5


Linux provides three types of I/O devices to the program:

Standard input (STDIN)-0 accepts input from the keyboard by default, standard output (STDOUT)-1 Default output to terminal window, standard error (STDERR)-2 default output to terminal window;

Since Linux provides us with three standard IO devices that do not meet all the circumstances, so there is an IO redirect, the file output stream directed to the specified file, terminal, you can now receive from different files, terminal input.



2. Output redirection

Includes standard output redirection and standard error output redirection.

StdOut and stderr can be redirected to a file:

Syntax: command action symbols supported by the symbol file name: > redirect the stdout to the file; 2> redirects the stderr to the file; &> redirects all outputs to the file;

2.1, output redirection classification:

>: The contents of the file will be overwritten; Set–c prohibits overwriting existing files, but can append >| File mandatory override set +c allow overwrite; >>: original content, append content; 2>: Overwrite redirect error output data stream; 2>>: Append redirect error output data stream


2.2, the standard output and error outputs are directed to different positions;
COMMAND >/path/to/file.out 2>/path/to/error.out


2.3. Merge standard output and error output redirect to the same data stream

&>: Overwrite redirection &>>: Append redirect >, 2>&1 and >> 2>&1:command >/path/to/file.out 2>& 1 (order is important) COMMAND >>/path/to/file.out 2>&1 (): Merge stdout of multiple programs (CAL; cal) > All.txt


3. TR command

TR: convert and delete characters;

TR [OPTION] ... SET1 [SET2];


Options:

-c–c--complement: Takes the complement of the character set;-D--delete: Deletes all characters belonging to the first character set;-S--squeeze-repeats: The characters that are repeated are represented by a single character;-T--truncate -set1: Converts the corresponding character of the first character set to the corresponding character of the second character set;

Wildcard characters supported by the TR command:

[: Alnum:]: Letters and numbers; [: Alpha:]: alphabetic; [: Cntrl:]: Control (non-printing) character; [:d Igit:]: number; [: graph:]: graphic character; [: Lower:]: lowercase letters; [:p rint:]: Printable character, [:p UNCT:]: punctuation; [: Space:]: blank character; [: Upper:]: Uppercase; [: xdigit:]: hexadecimal character;


3.1. Import stdin from the file


(1) Use < to redirect the standard input, can be a file can be keyboard input;


(2) Some commands can accept stdin from file import, such as cat/etc/passwd equivalent to #cat </etc/passwd, and for input redirection that cannot be accepted from a file, use < to specify input redirection.

Tr ' A-Z ' A-Z </etc/issue: This command converts lowercase characters in/etc/issue to write characters tr–d ABC </etc/fstab delete any characters from all ABC in fstab file; # # cat/etc/ passwd equivalent to #cat </etc/passwd;



(3) Accept the standard input from the keyboard by default:

Cat > Fileabcdef Press Ctrl+d to leave, you can use the file instead of the keyboard input


(4) Accept the standard input from the keyboard and redirect the output to a file:
?

Cat > Filea < Fileb


3.2, the multi-line sent to stdin;

Use the << stop Word command to redirect multiple lines from the keyboard to stdin until all text in the terminating word position is sent to stdin; sometimes referred to as in-place text (Heretext).

The Mail command is the default to receive standard input;

Mail-s "Please call" [email protected] <<END> Hi wang,> >please give me a call when you get in. We may need> to does some maintenance on server1.> >details when you ' re on-site> zhang> END


4. Pipeline

The input-output redirection function is powerful, but it cannot resolve the input result of a command directly to a command, as a result of the execution of a command, but only the input results can be directed to the current terminal or the specified file;


Format:

Command 1 | Command 2 | Command 3 |    ... Description: Pipe (using the symbol "|" Used to connect the command:


Role:

Send stdout of command 1 to stdin of command 2, stdout of command 2 to stdin of command 3;

StdErr By default cannot be piped forward and can be implemented using 2>&1 or |&;
The last command executes in the child shell process of the current shell process;

A function used to combine multiple tools; LS | Tr ' A-Z ' A-Z

[[Email protected] ~] #ls/a |& tr ' A-Z ' A-Z ' ls:cannot access/a: NO SUCH FILE OR directory[[email protected] ~] #ls/ A | Tr ' A-Z ' A-Z ' ls:cannot access/a: No such file or directory

4.1. Common view commands in pipelines:


Less: Check the input one page at a page, exit after reading, and accept standard keyboard input by default. However, using less in the pipeline will result in the inability to use the "B" key to complete the rollover.

Ls-l/etc | Less


Mail: Send input by email, receive standard keyboard input by default;

echo "Test Email" | Mail-s "Test" [email protected]


LPR: Sends the input to the printer:

echo "Test print" | Lpr-p Printer_name


4.2, in the pipeline-symbol


Example:
Packaging the files inside/home, but the packaged data is not recorded to the file, but sent to the stdout, after the pipeline, the TAR-CVF-home to the back of the TAR-XVF-, the latter-is to take the previous command of the stdout, so there is no need to make Use the temp file.

TAR-CVF-Home | TAR-XVF-


4.3. Redirect to multiple targets (tee)


Format:

Command 1 | Tee [-A] file name | Command 2
Explain:

Save the stdout of command 1 in the file as input to command 2;-a append.
Function:

Save output at different stages, troubleshoot complex pipelines, and view and record output at the same time;

Demonstrate:

[[Email protected] ~] #ls-A | Tee/app/ls.out | Tr ' A-Z ' A-Z '. Bash_history. Bash_logout. Bash_profile. Bashrc[[email protected] ~] #cat/app/ls.out. BASH_HISTORY.BASH_LOGOUT.BASH_PROFILE.BASHRC

Linux Basic Management-"standard IO and redirects and pipelines"

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.