Linux command redirection >, >>, 1>, 2>, 1>>, 2>>, <

Source: Internet
Author: User
Tags python script

REDIRECT command actually use a lot of it, just do not look carefully, this wave just again use and empty summed up a wave.

See > and >> First:

The only difference between the two is that the > is redirected to a file,>> is the append content to the file. Two commands are created if a file does not exist. Take an example to see:

(THRIFT_RPC) Piperck?  Desktop  ?  123 . txt (thrift_rpc) Piperck?  Desktop  ?   grep 123 . txt-rw-r--r--   1 Piperck staff   2:  - 123. txt

The

can see that ll's output can be redirected to 123.txt, and > is the same effect. Just if you use > to perform the redirect location without this file

(THRIFT_RPC) Piperck?  Desktop  ?  ls >/poweqpikp/123file or directory:/poweqpikp/123(THRIFT_RPC ) Piperck?  Desktop  ?   ls >>/poweqpikp/123file or directory:/poweqpikp/123 (THRIFT_RPC) Piperck?  Desktop  ?   ls 123 (THRIFT_RPC) Piperck?  Desktop  ?   ls 1234

But if you redirect the files in the folder without the folder, then the folders he will not help you create, so will report errors, this point to pay attention to.

Then the use of 1>, 2>, 1>>, 2>>,

When we need to put the correct output of a file, and the wrong output to save the time we can use them. Let me do an experiment with a simple Python script

The Python script is as follows

# Coding:utf-8 if __name__ ' __main__ ' :     Print ' Gogogog '     Int ('qiowjeioqwjeo')

The script will correctly hold the print Gogogog and then report the following strong turn error.

(THRIFT_RPC) Piperck?  Py_pra? Python pp.py 1> right.txt 2>wrong.txt (THRIFT_RPC) Piperck?  Py_pra? Lsns8a_client gevent pp.py starcraft2baidu_phoneassist laplace_pra right.txt WR  Ong.txt (THRIFT_RPC) Piperck?  Py_pra?  Cat Right.txtgogogog (THRIFT_RPC) Piperck?  Py_pra? Cat Wrong.txttraceback (most recent): File"pp.py", line 6,inch<module>Int ('Qiowjeioqwjeo') valueerror:invalid literal forInt () with base 10:'Qiowjeioqwjeo'

As you can see, the correct output is saved into the right.txt and the wrong output is redirected to Wrong.txt.

So what happens if we don't differentiate between the redirection rules of 1>2> and the running results?

(THRIFT_RPC) Piperck?  Py_pra  ?  Python pp.py > pp.txttraceback (most recent):  'pp.py' in <module>    Int ('qiowjeioqwjeo' for ' Qiowjeioqwjeo ' (THRIFT_RPC) Piperck?  Py_pra  ?  Cat Pp.txtgogogog

You can see that only the correct results are saved, and the wrong information is printed on the standard output.

So what should we do if we want to redirect the error and the correct result to the same file?

(THRIFT_RPC) Piperck?  Py_pra  ?  Python pp.py 1> ppp.txt 2>&1(THRIFT_RPC) Piperck?  Py_pra  ?  Cat Ppp.txtgogogogtraceback (Most  recent):'pp.py' in < module>    Int ('qiowjeioqwjeo'for'  qiowjeioqwjeo'

As you can see, we also redirect the error to the same file as 1 to achieve this requirement.

The same 1>> 2>> is actually append data to the file, and the previous introduction of > >> is not different, it is necessary to mention that if we want to redirect the wrong and correct information to the same file should be how to do? You might think of 2>>&1. The reality, however, is that there is no such grammar.

However, we can implement this function using 1>>pp.txt 2>&1 syntax

(THRIFT_RPC) Piperck?  Py_pra? Python pp.py 1>> pp.txt 2>&1(THRIFT_RPC) Piperck?  Py_pra? Cat Pp.txtgogogoggogogogtraceback (most recent): File"pp.py", line 6,inch<module>Int ('Qiowjeioqwjeo') valueerror:invalid literal forInt () with base 10:'Qiowjeioqwjeo'Gogogogtraceback (most recent): File"pp.py", line 6,inch<module>Int ('Qiowjeioqwjeo') valueerror:invalid literal forInt () with base 10:'Qiowjeioqwjeo'

It seems that 1> 1>> 2> 2>> is one by one corresponding, but in fact not, they can be mixed, for example the correct results want to append, the wrong result I want to overwrite.

(THRIFT_RPC) Piperck?  Py_pra? Python pp.py 1>>right.txt 2>wrong.txt (THRIFT_RPC) Piperck?  Py_pra?  Cat Right.txtgogogog (THRIFT_RPC) Piperck?  Py_pra? Cat Wrong.txttraceback (most recent): File"pp.py", line 6,inch<module>Int ('Qiowjeioqwjeo') valueerror:invalid literal forInt () with base 10:'Qiowjeioqwjeo'(THRIFT_RPC) Piperck?  Py_pra? Python pp.py1>>right.txt 2>Wrong.txt(THRIFT_RPC) Piperck?  Py_pra?  Cat Right.txtgogogoggogogog (THRIFT_RPC) Piperck?  Py_pra? Cat Wrong.txttraceback (most recent): File"pp.py", line 6,inch<module>Int ('Qiowjeioqwjeo') valueerror:invalid literal forInt () with base 10:'Qiowjeioqwjeo'

Similar to this combination of mixed cases, is relatively not uncommon. You can also see the flexibility of redirecting identities.

So if we want to save the correct result, the wrong result is thrown directly to the dumpster, neither saved as a file nor printed in standard output.

(THRIFT_RPC) Piperck?  Py_pra  ?  Python pp.py 1>>right.txt 2>/dev/null (THRIFT_RPC) Piperck?  Py_pra  ?  Cat Right.txtgogogoggogogoggogogog

REDIRECT error output directly to/dev/null he's like a bottomless pit, and the stuff he throws in is gone.

So what does < mean? < can change from a standard input to a specified place. For example, it is clear that:

(THRIFT_RPC) Piperck?  Py_pra  ?  VI hh (THRIFT_RPC) Piperck?  Py_pra  ?   >> txt.py < hh (thrift_rpc) Piperck?  Py_pra  ?  Cat Txt.pyqwejqwoijeoq

Flexible use of redirect identifiers will greatly facilitate our daily operation of the system, and flexible control of the operation of the script, so be sure to master yo!

Reference:

http://blog.chinaunix.net/uid-298599-id-2443108.html Linux-command redirection

Linux command redirection >, >>, 1>, 2>, 1>>, 2>>, <

Related Article

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.