In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the method of linux input and output redirection". In the daily operation, I believe that many people have doubts about the method of linux input and output redirection. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of linux input and output redirection. Next, please follow the editor to study!
1. Output redirection:
By default, both standard output and error output are terminals, and standard output and error content can be redirected:
The copy code is as follows:
[~] # echo "hello\!"
Hello\!
[~] # echo "hello!"
-bash:! ": event not found"
Redirect standard output to a file
The copy code is as follows:
[~] # echo "hello" > test.sh
[~] # cat test.sh
Hello
'>' output mode is equivalent by default'1 >'
[~] # echo "hello" 1 > test.sh
[~] # cat test.sh
Hello
However, the error content is still displayed on the screen:
The copy code is as follows:
[~] $cat edit.sql / root/test.sh > temp.sh
Cat: / root/test.sh: permission denied
You can also output the error content to a file (using file descriptors):
The copy code is as follows:
[~] $cat edit.sql / root/test.sh 1 > temp.sh 2 > error.sh
[~] $cat temp.sh
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
[~] $cat error.sh
Cat: / root/test.sh: permission denied
Write standard output and error messages to the same file:
The copy code is as follows:
[~] $cat edit.sql / root/test.sh > temp.sh 2 > & 1
[~] $cat temp.sh
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
Cat: / root/test.sh: permission denied
This looks troublesome, but the most commonly used ones in practical applications are:
The copy code is as follows:
[~] $cat edit.sql / root/test.sh & > temp.sh
[~] $cat temp.sh
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
Cat: / root/test.sh: permission denied
& > write all output to the same file
If you do not want to export to a file or display it on the screen, you can take advantage of the special device file / dev/null (bit bucket).
[~] $cat edit.sql / root/test.sh & > / dev/null
If you write standard output to a file, you will not be able to use the pipe symbol'|'to pass the content to the following command. You can use the tee command to solve this problem:
The copy code is as follows:
[~] $cat edit.sql / root/test.sh | tee temp.sh | cat-n
Cat: / root/test.sh: permission denied
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
The tee command is equivalent to writing a copy of stdout to a file, and then passing stdout to the next command, but the error content cannot be passed by tee. Just like using > > to append content, tee can append it with the-an option:
The copy code is as follows:
[~] $cat edit.sql / root/test.sh | tee-a temp.sh | cat-n
Cat: / root/test.sh: permission denied
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
[~] $cat temp.sh
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
Select dbms_rowid.rowid_object ('aaazdqaagaaatxjaak') data_object_id#
Dbms_rowid.rowid_relative_fno ('aaazdqaagaaatxjaak') rfile#
Dbms_rowid.rowid_block_number ('aaazdqaagaaatxjaak') block#
Dbms_rowid.rowid_row_number ('aaazdqaagaaatxjaak') row# from dual
/
two。 Enter redirection:
Input redirection is often used in some situations, such as database monitoring, and inline redirection is commonly used.
The copy code is as follows:
[~] $cat
< edit.sql select dbms_rowid.rowid_object('aaazdqaagaaatxjaak') data_object_id#, dbms_rowid.rowid_relative_fno('aaazdqaagaaatxjaak') rfile#, dbms_rowid.rowid_block_number('aaazdqaagaaatxjaak') block#, dbms_rowid.rowid_row_number('aaazdqaagaaatxjaak') row# from dual / 例如,如下操作,把 ) 创建一个文件描述符3,用于打开文件 [~]# exec 3< test.sh 下面就可以直接使用文件描述符打开文件了,但是只能使用一次: 复制代码 代码如下: [~]# cat test.sh [~]# echo okok >& 4
[~] # cat test.sh
Okok
This is actually similar to the previous one:
The copy code is as follows:
[~] $cat edit.sql / root/test.sh > temp.sh 2 > & 1
Create a file descriptor 5 to append content to the file (it can also be reused, unlike the input file descriptor, which can only be used once):
The copy code is as follows:
[~] # exec 5 > > test.sh
[~] # echo okokok > & 5
[~] # cat test.sh
Okokok
[~] # echo okokok > & 5
[~] # cat test.sh
Okokok
Okokok
At this point, the study of "methods used for linux input and output redirection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.