IO Redirection and small experiments

Source: Internet
Author: User
Tags gstreamer

IO Redirection and small experiments


Standard inputs and outputs

IO redirection

TR, tee Command and piping

First of all, what is I/O redirection, so-called I/O redirection is simply a process that captures the output of a file, or command, program, script, and then sends the captured output as input to another file, command, program, or script.

When it comes to I/O redirection, it involves a file identifier, in a Linux system, the system specifies a file identifier for each open file for the system to track the file, the file identifier is a number, different numbers represent different meanings, by default, the system occupies 3, 0 standard input (stdin), 1 standard output (STDOUT), 2 standard error (STDERR), respectively.

Standard input usually refers to keyboard input

Standard output usually refers to the output of the display

Standard errors are usually also directed to the monitor


Output redirection and Error output redirection:


StdOut and stderr can be redirected to a file: The command action symbol file name supports the operation symbols including:

> redirect stdout to a file

[[email protected] ~]# ls > f1[[email protected] ~]# cat F1anaconda-ks.cfgdesktopdocumentsdownloadsf1install.loginstall.log.syslogmusic here the results of the LS command output to the file F1, the original display on the display results output to F1. Then view the results of the F1 file as the result of the LS command


2> redirect stderr to file

[[email protected] ~]# ls/tttls:cannot access/ttt:no such file or Directory[[email protected] ~]# ls/ttt 2> f1[[em AIL protected] ~]# cat f1ls:cannot access/ttt:no such file or directory here we first output an error result with the LS command to redirect the result of Ls/ttt to a file F1 view the F1 file The results


&> Redirect all output to file

[[email protected] ~]# ls/root/ttt &> f1[[email protected] ~]# cat f1ls:cannot access/ttt:no such file or dire Ctory/root: Anaconda-ks.cfgdesktopdocumentsdownloadsf1install.loginstall.log.syslogmusicpicturespublictemplatesvideos use &> can direct the correct and incorrect output to the file F1.


the standard output and error outputs are directed to different locations:

COMMAND >/path/to/file.out 2>/path/to/error.out

Take a look at the following example:

[Email protected] ~]# ls/ttt/root > F1.out 2> f2.out [[email protected] ~]# cat F1.out/root:anaconda-ks.cfgdesk Topdocumentsdownloadsf1.outf2.outinstall.loginstall.log.syslogmusicpicturespublictemplatesvideos[[email Protected] ~]# cat f2cat:f2:No such file or directory here we find Ls/root is the correct output, f1.out inside the file ls/ttt for the error output, inside the document F2.out


Note: the > to file will overwrite the file contents

Use Set-c to disallow overwriting of content to an existing file, but can be appended to a file

Use set +c to allow overwriting

Forced Overwrite: >|

>> additional content based on the original content

2> Overwrite redirect error output data stream;

2>>-weighted directional error output data stream;

Merge standard output and error output redirect for the same data stream:

&>: Overwrite redirect

&>>: Append redirect

(): Merging stdout of multiple programs

[Email protected] ~]# (ls/root; cat/etc/issue) > F1[[email protected] ~]# cat F1ANACONDA-KS.CFGDESKTOPDOCUMENTSDOWNL Oadsf1f1.outf2.outinstall.loginstall.log.syslogMusicPicturesPublicTemplatesVideosCentOS Release 6.8 (Final) Kernel \ r on an \mmage education learning serviceshttp://www.magedu.com

Pipeline:


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


Pipeline is a very important way of communication in Linux, which is to connect the output of one program directly to the input of another program. The pipe command operator is: "|", it can only handle the correct output information through the previous instruction, that is, the standard output information, for the Stdandard error information does not directly handle the ability. Then, pass to the next command as standard input.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/51/wKioL1egC8vT91DNAAATzVYhtlQ007.png "title=" Image_ 2.png "alt=" Wkiol1egc8vt91dnaaatzvyhtlq007.png "/>

Command1 correct output, as input of command2 and then comand2 output as, comand3 input, Comand3 output will be displayed directly on the screen.


TR command:

TR is used to convert characters from standard input through substitution or deletion operations.

Usage:

TR [OPTION] ... SET1 [SET2]

Common options:

Options:

-C or--complerment: a complement to the character set

-D or--delete: Deletes all characters belonging to the first character set;


Here we use the-C with the-D option to give an example:

[Email protected] ~]# echo 12bchsa333nd | TR-CD ' [0-9] ' 12333[[email protected] ~]# We use the echo output to send a string of characters through the pipeline to TR, using the-C (complement)-D (delete) to delete the characters other than [0-9]

-S or-squeeze-repeats: the consecutive repeating character is represented by a single character

[email protected] ~]# cat F1aaaaaaabbbbbbbccccccc[[email protected] ~]# Cat F1 | Tr-s "ABC" ABC


-T or--truncate-set1: First remove characters from the first character set that are more than the second character set

[Email protected] ~]# echo ABCDE | Tr ' a-d ' xyz ' xyzze after replacement is not enough with the last character replacement [[email protected] ~]# echo ABCDE | Tr-t ' a-d ' xyz ' xyzde after substitution of characters is not enough to replace


Tee command:

Command 1 | Tee File name | Command 2

Save the stdout of command 1 in the file name, and then pipe the input to the command 2

Use:

Save output at different stages

Troubleshooting Complex Pipelines

View and record output at the same time

[Email protected] ~]# Ls/root | Tee F1 | Cat >> F1[[email protected] ~]# cat F1anaconda-ks.cfgdesktopdocumentsdownloadsf1f1.outf2.outf3install.loginstall.log.syslogmusicpicturespublictemplatesvideos Anaconda-ks.cfgdesktopdocumentsdownloadsf1f1.outf2.outf3install.loginstall.log.syslogmusicpicturespublictemplatesvideos

Enter redirection:

Import stdin from a file

Use < to redirect standard input

Use < to redirect standard input

Some commands can accept stdin imported from a file:

Tr ' A-Z ' A-Z ' </etc/issue

[[Email protected] ~]# TR ' A-Z ' A-Z ' </etc/issuecentos RELEASE 6.8 (FINAL) KERNEL \ R on an \mmage education learning S The erviceshttp://www.magedu.com command converts lowercase characters in/etc/issue to uppercase characters


TR–D ABC </etc/fstab

[[email protected] ~]# tr -d  ' A-Z '  < /etc/fstab ## //# c     M J 25 09:40:54 2016## A ,  ,      '//' # s    (5),  (8),  (8)  /  (8)    #///0-     /                        4             1 1UUID=95470-1451-408-9902-4032569 /                    4             1 2///0-     /                     4             1 2///0-     /                     4             1 2///0-                                      0 0                    //                             0 0                   //                  &nBsp;=5,=620  0 0 


#cat > Filea < Fileb

[[email protected] ~]# cat > F1sdasdasdasdasdasdasd[[email protected] ~]# touch F3[[email protected] ~]# cat > F3 &L T;f1[[email protected] ~]# cat F1sdasdasdasdasdasdasd[[email protected] ~]# cat F3SDASDASDASDASDASDASD Press Ctrl+d to leave, You can use the file instead of the keyboard input cat > F1 use the cat input character to the F1 file Cat > F3 <f1 Use the cat command to get input from the F1 file and output it to the F3 file.

Multi-line REDIRECT <<eof

Use the << stop Word command to redirect multiple lines from the keyboard to stdin

Mail uses multi-line redirection

[[email protected] ~]$ mail -s test root <<eof> hello,>  this is test > hahaha...> eof[[email protected] ~]#  Mailheirloom mail version 12.5 7/5/10.  type ? for help. " /var/spool/mail/root ":  2 messages 2 new>n  1 [email protected]   Mon Jul 25 11:03 1010/46619  "[Abrt] full crash report"  n  2 [email protected]  mon jul 25 11:34  20/617     "Test" & 2Message  2:From [email protected]  Mon  jul 25 11:34:47 2016return-path: <[email protected]>x-original-to:  rootdelivered-to: [email protected]date: mon, 25 jul 2016 11:34:46 + 0800to: [email protected]subject: testuser-agent: heirloom mailx 12.5 7/5/10content-type: text/plain; charset= us-asciifrom: [email protected]status: rhello,this is test hahaha...&


The difference between a pipeline and a redirect is:


1, the left command should have standard output | The command on the right should accept the standard input

The command on the left should have standard output > the right can only be a file

The command on the left should require standard input < The right can only be a file


2, Pipeline trigger two sub-Process Execution "|" On both sides of the program, and redirection is performed within a process

Small experiment

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

[Email protected] ~]# Cat/etc/issue | tr [A-z] [a-z] \skernel \ r on an \m

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

[email protected] ~]# who |     tr [A-z] [a-z] >/tmp/who.out[[email protected] ~]# cat/tmp/who.out root pts/0 2016-07-25 11:04 (: 0) root           TTY2 2016-07-25 11:03root:0 2016-07-25 11:04 (: 0) Root pts/2 2016-07-25 11:06 (10.1.250.72) root PTS/3 2016-07-25 11:32 (10.1.250. 72)


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,pleasehelp me to check it, thanks!

Operating system Version Information

[[Email protected] ~] $echo-E "hello,i am ' WhoAmI ', the version is here, pleasehelp me to chick it, thanks! /n ' lsb_release ' | Mail-s "Help" root

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

[[email protected] ~]# echo $ (ls -a)  > &NBSP;F1[[EMAIL&NBSP;PROTECTED]&NBSP;~]#&NBSP;CAT&NBSP;F1.&NBSP, ....  anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache . config .cshrc desktop documents downloads .esd_auth f1 .gstreamer-0.10  .gtkrc-2.0-kde4 .kde .local Music Pictures Public .ssh .tcshrc  templates videos . xauthority[[email protected] ~]# ls -a | tr  ' \ n '   '   '.    anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache . config .cshrc desktop documents downloads .esd_auth f1 .gstreamer-0.10  .gtkrc-2.0-kde4 .kde .local Music Pictures Public .ssh .tcshrc  templates videos . Xauthority 


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

[[email protected] ~]# echo $[1+2+3+4+5+6+7+8+9+10]55[[email protected] ~]# echo "1 2 3 4 5 6 7 8 9 10" | Tr ' + ' |bc55[[email protected] ~]# echo $[' echo ' 1 2 3 4 5 6 7 8 9 "| tr" + "]55

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

[Email protected] ~]# lsanaconda-ks.cfg Desktop Documents Downloads F1 Music Pictures public Templates Videos Z Zzzz.txt[[email protected] ~]# cat zzzzz.txt-ainode^m$reference^m$contents^m$directory^m$^m$list directory entries Instead of contents, and do not dereference symbolic links^m$[[email protected] ~]# cat Zzzzz.txt | Tr-d ' \ R ' >f1[[email protected] ~]# cat f1-ainode$reference$contents$directory$ $list directory entries instead of con Tents, and do not dereference symbolic links$


7, processing string "Xt.,l 1 jr#! $mn 2 c*/fe3 uz4", keep only the numbers and spaces


[[email protected] ~]# echo ' xt.,l 1 jr#! $mn 2 c*/fe3 uz4 ' | TR-CD ' [:d igit:] [: Space:] ' 1 2 3 4

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

[Email protected] ~]# echo $PATH/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [Email protected] ~]# echo $PATH | Tr ': ' \ n '/usr/lib64/qt-3.3/bin/usr/local/sbin/usr/local/bin/usr/sbin/usr/bin/root/bin

9. Delete blank lines of the specified file

[[email protected] ~]# cat f1-ainode$$ $reference $ $contents $$ $directory $ $list directory entries instead of contents, and D o Not dereference symbolic links$[[email protected] ~]# Cat F1 | Tr-s ' \ n ' inodereferencecontentsdirectorylist directory entries instead of contents, and do not dereference symbolic links


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

[Email protected] ~]# Cat/etc/init.d/functions | Tr-cs ' [: Alpha:] ' \ n '







---


This article is from the "Zanghl_blog" blog, make sure to keep this source http://zanghl.blog.51cto.com/11267212/1833416

IO Redirection and small experiments

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.