IO Redirection and piping
Always mentioned, program: Instruction + data
In fact, the program also has IO, the source of data has many places: file, external
Devices available for input: Files (all Linux files)
Keyboard device, file system on the regular file loading content, network card, etc.;
Devices available for output: Files (all Linux files)
Monitor, file system on the regular file output save, network card, etc.;
There are three types of data flow in the program:
input data stream; <--standard input (stdin), is the keyboard;
Output data Flow:--> standard output (stdout), display;
Error output stream:--Error output (stderr), display;
Fd:file descriptor, file descriptor; the corresponding identifier is as follows:
Standard input: 0
Standard output: 1
Error Output: 2
IO redirection:
output redirection:>
Features: Overwrite output, overwrite anaconda-ks.cfg file contents into Kk.txt
[Email protected]_1 ~]# cat anaconda-ks.cfg > Kk.txt
output redirection:>>
Features: Append output, add from tail of file
[Email protected]_1 ~]# cat anaconda-ks.cfg >> kk.txt
Error Output stream redirection:2>,2>> (Append)
[Email protected]_1 sh]# cat Test.txt 2> Error.log
[Email protected]_1 sh]# Cat Error.log
Cat:test.txt:No such file or directory
[Email protected]_1 sh]# cat Test.log 2>> Error.log
[Email protected]_1 sh]# Cat Error.log
Cat:test.txt:No such file or directory
Cat:test.log:No such file or directory
merge Normal output stream and error output stream:
The merge output stream also has two ways of overwriting and appending:
(1) &>, &>>
(2) COMMAND >/path/to/somefile 2>&1
COMMAND >>/path/to/somefile 2>&1
[Email protected] tmp]# cat/root/anaconda-ks.cfg >> an.cfg 2>&1
Note : Viewing the contents of the Anaconda-ks.cfg file is appended to the an.cfg, and if an error output is appended to the An.cfg file;
Mention Special equipment:/dev/null
This special device is understood as a "data black hole", and all data thrown in it will be deleted.
Set is shell internal command, you can set prohibit overwrite output redirect to existing file; (Centos 6)
[Email protected] ~]# set-c
[email protected] ~]# Cat fstab > issue
-bash:issue:cannot Overwrite existing file
If you cannot use the redirection feature when it is turned on, you can use the force overwrite output at this time: >|:
[email protected] ~]# cat Fstab >| Issue
Turn off these features:
[Email protected] ~]# set-c
Note: Set settings are only valid for the current shell
input Redirect:<
Input redirection is the data input source for the command, which does some work on the data.
Let's start with a command:
TR command:
Tr-translate or delete characters
Format:
TR [OPTION] ... SET1 [SET2]
Converts the characters in the input data to all occurrences within the SET1 definition, with all one by one pairs of bits converted to SET2 characters
Options:
-D: Deleting a character Set
Example 1:
TR SET1 SET2 </path/from/somefile
[Email protected]_1 sh]# TR s S </issue
\s
Kernel \ r on an \m
NOTE: Convert all uppercase s in issue to lowercase s
Example 2:
Tr-d SET1 </path/from/somefile
[Email protected]_1 sh]# tr-d S <./issue
\
Kernel \ r on an \m
Note: Do not modify the original file
here document (created here):<<
Terminator Operation two ways:
Cat << EOF
Cat >/path/to/somefile << EOF
Example:
[[Email protected]_1 sh]# Cat << EOF
> Hello
> EOF
Hello
Explain:
EOF is a terminator that exits when there is EOF in the input content
Pipeline:
A connection program that implements the output of the previous command directly after a program as an input data stream
Can be multi-level connection:
COMMAND1 | COMMAND2 | COMMAND3 | ...
Pipe symbol: |
[[Email protected]_1 sh]# cat/etc/issue | TR Cos cos
Tee command:
Bidirectional pipeline, meaning that the input data stream is retained and a program is used as the input data stream in the secondary
Format:
COMMAND | Tee/path/to/somefile
Example:
[Email protected] ~]# Cat/etc/issue | Tee KKK | Tr-d ' 6.4 '
CentOS Release (Final)
Kernel \ r on an \m
[email protected] ~]# cat KKK
CentOS Release 6.4 (Final)
Kernel \ r on an \m
Exercise 1: Convert the information from the first 6 lines of the/etc/passwd file into uppercase characters after output;
[Email protected] ~]# Head-n 6/etc/passwd | Tr ' A-Z ' A-Z
This article is from the "Disguised Geek" blog, please be sure to keep this source http://waldenkk.blog.51cto.com/2410270/1929948
Getting Started with Linux Basics--io redirects and pipelines