Input and output redirection and pipeline commands

Source: Internet
Author: User

input and output redirection, piping

1. File descriptor :

FD: The kernel is used to track the properties (identities) of files that have been opened in memory, is a memory structure,

Need space to hold this information

2. Standard input and output

Input: Standard input: stdin, Descriptor: 0

Ouput: Standard output: stdout 1

Error Output: stderr 2

3. I/O redirection

1). Input redirection: < (enter the concept of no overlay)

<< [EOF package]: Create a file here

Often used to create files in scripts or to generate menus

Eg: #生成菜单

Cat << EOF

A:show A

B:show b

C:show C

Eof

--------> A:show A

B:show b

C:show C

#把脚本生成的信息输出生成一个文档

Cat "/path to file.txt << EOF

A:show A

B:show b

C:show C

Eof

2). Output Redirect: > (overwrite Output) >> (append output, not overwrite original content)

3). Force Overwrite redirect output: >|

4). Error redirection: 2> (Overwrite output), 2>> (Append output)

5). All Information redirection: Merge standard output and error output redirect for the same data stream

&>: (both correct and error messages are directed)

equivalent to cmd >/path 2>&1

&>>: Append redirect

6). Supports simultaneous redirection of the correct and error outputs of the standard:

cmd >/path to 1 2>/path to 2 (correct and error messages are output to the specified file respectively)

7). (): Merge stdout of multiple programs (with; semicolon split)

(Cal ; CAL) > All.txt

8). Multi-line redirection: With <<eof .... EOF Package

Eg:cat F1 <<eof

Kdkdk

Kdkdk

EOF (must be a separate line, no spaces, etc.)

------for redirection

9). Disable override redirection feature: Set-c

Reply feature: Set +c

Force Overwrite redirect output: >| (Force override redirection when No Override has been set)

Note: Output redirection can only be used for normal output and cannot be used for error output

)./dev/null bit bucket: bits to discard any data entered into it

11). Command execution status Result: Use $?

Execute correctly: 0 Error: not 0 value

Custom exit:

Exit [n]

4. Pipeline: a special implementation similar to output redirection, can achieve multiple use

1). piping (using the symbol "|" Used to connect commands

Command 1 | Command 2 | Command 3 | ...

Send stdout of command 1 to stdin of command 2, command 2.

StdOut sent to command 3 of stdin

stderr cannot be piped forward by default and can be implemented with 2>&1 or |&

The last command executes in the child shell process of the current shell process to

Note: The variables generated by the preceding command cannot be passed and passed to the final command, and the final command is in the child

in the shell run, is no longer in the scope of the preceding build variable

StdErr cannot be piped forward by default and can be implemented with 2>&1 or |&

2). Implementation in the pipeline is output to the display end and output to the following command: T-pipe

Command Tee

Usage: Immediately following the command that requires bidirectional output

Eg:cat/file | TEE/TMP/ABCD | Wc-l #显示file的内容并且放在/TMP/ABCD

Output to the WC command displays the number of rows

3. TR: Convert or delete

Format: TR [option] SET1 [SET2]

-C complement set (except for content that matches content)

-D Delete characters that match SET1

-S compression, which compresses the repeated characters to a single character display

-T when corresponding to the conversion, the number of digits behind the next character group is more than the preceding hours, only the number of digits corresponding to the following character group is converted.

More out of whatever

\ t Horizontal tab

\v vertical tab allows for ladder-like

Corresponding conversion: ' ABCD ' ' efgh '

A->e B->f, etc.

' ABCD ' ' EFG '

(Note: The last G is used as the conversion object of the previous d when the number of characters in the following is insufficient)

The habit of > followed by a file, | followed by the command, if the > followed by the command, it may be the command as a file name.


Practice:

1. Convert the contents of the/etc/issue file to uppercase and save to the/tmp/issue.out file

Cat/etc/issue | TR "A-Z" "A-Z" >/tmp/issue.out

2. Convert the current system login user's information to uppercase and save to the/tmp/who.out file

WhoAmI | TR "A-Z" "A-Z" >/tmp/who.out

3, a Linux user to the root email, request the message titled "Help", the message body is as follows:

Hello, I am Username, the system version is here,please help me to

Check it, thanks!

Operating system Version Information

Echo-e "Hello, I am ' whoami ', the system version is Here,please help me to check it, thanks! \ n

' Lsb_release ' > mail-s ' Help ' Liux

4, the/root/file list, displayed as a line, and the file name separated by a space.

Ls-m/root | TR, \b

Or: Ls/root | Tr ' \ n '

5. The contents of the File1 file are: "1 2 3 4 5 6 7 8 9 10" Calculates the sum of all numbers

echo "1 2 5 6 7 8 9" > File1

echo $[cat file1 | tr "+"

6. Remove the ' ^m ' character from the Windows text file

Cat A.txt | Tr-d ' \ R ' (in Windows, the file line is displayed as ^m)

7, processing string "Xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4", keep only the numbers therein

and spaces

echo 'xt.,l 1 jr#! $mn 2 c*/fe3 uz4' | TR-CD ' [:d igit:] \ n '

8. Display the path variable in a separate row for each directory

echo $PATH | TR: ' \ n '

9. Delete the duplicate blank lines in the specified file

Cat File | Tr-s ' \ n '

10. Display each word (letter) in the file in a separate line with no blank lines

Tr-cs ' [: Alpha:] ' \ n '


Input and output redirection and pipeline commands

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.