Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve redirection in linux

Shulou Source: shulou.com Published: 2022-06-02 00:37:37 09月27日 Update

This article is about how linux can be redirected. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Linux redirection

First look at > and > >, their two functions are similar, they are output to a file, the only thing is that > will overwrite the original content of the file, and > > is appended after the original content.

Then there is the use of 1 >, 2 >, 1 > >, 2 > >, which is used when the correct output and error output need to be saved separately. For example, when executing a script. Use a simple python script to explain:

The script is as follows:

# coding: utf-8if _ _ name__ = ='_ _ main__': print 'Hello' int (' Hello')

The script first outputs "Hello", which is normal, and then converts "Hello" to int type, where an error is reported. Run directly:

$python test.py > txtTraceback (most recent call last): File "test.py", line 4, in int ('Hello') ValueError: invalid literal for int () with base 10:' Hello'$cat txtHello

You can see that the correct output is saved to the result.txt file, while the error message is output to the screen.

Do the following:

Python test.py 1 > right.txt 2 > wrong.txt$cat right.txtHello$cat wrong.txtTraceback (most recent call last): File "test.py", line 4, in int ('Hello') ValueError: invalid literal for int () with base 10:' Hello'

The correct output is saved to right.txt and the error output is saved to wrong.txt

1 > > and 2 > > and > > are similar, adding new content to the original content.

Thank you for reading! This is the end of the article on "how to redirect linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Output content script file is in error function more article good practical one line information screen article new content see knowledge type face Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Xiaomi Microsoft Shulou Technology