linux输入输出重定向使用详解
1. 输出重定向:
默认条件下,标准输出和错误输出都是终端,可以把标准输出和错误内容进行重定向:
代码如下:
[~]# echo "hello!"
hello!
[~]# echo "hello!"
-bash: !": event not found "
hello!
[~]# echo "hello!"
-bash: !": event not found "
把标准输出重定向到文件
代码如下:
[~]# echo "hello" > test.sh
[~]# cat test.sh
hello
'>'输出方式默认等价'1>'
[~]# echo "hello" 1> test.sh
[~]# cat test.sh
hello
[~]# cat test.sh
hello
'>'输出方式默认等价'1>'
[~]# echo "hello" 1> test.sh
[~]# cat test.sh
hello
但是错误内容还是会显示在屏幕上:
代码如下:
[~]$cat edit.sql /root/test.sh > temp.sh
cat: /root/test.sh: Permission denied
cat: /root/test.sh: Permission denied
可以把错误内容也输出到文件中(利用文件描述符):
代码如下:
[~]$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
[~]$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
把标准输出和错误信息写入同一个文件:
代码如下:
[~]$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
[~]$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
这个看起来比较麻烦,实际应用中可能用的最多的是:
代码如下:
[~]$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
&>把所有输出都写入同一个文件
[~]$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
&>把所有输出都写入同一个文件
如果不想输出到文件,也不想在屏幕上显示,可以利用/dev/null这一特殊设备文件(bit bucket)
[~]$cat edit.sql /root/test.sh &>/dev/null
如果把标准输出写入到文件,就没法利用管道符号'|'把内容传给接下来的命令,可以利用tee命令解决这个问题:
代码如下:
[~]$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
/
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
/
tee命令相当于把stdout副本写入文件,再把stdout传给下一个命令,但是错误内容是无法用tee传递的,如同使用>>追加内容,tee可以使用-a选项追加:
代码如下:
[~]$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
/
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
/
2.输入重定向:
输入重定向,在一些场合,例如数据库监控中用的比较多,常见的是使用内联重定向
代码如下:
[~]$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
/
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
/
例如,如下操作,把 <<EOF > temp.sh到最后EOF之间的内容当作stdin,然后把stdout写入文件temp.sh
代码如下:
[~]$cat <<EOF > temp.s
this is my log
EOF
[~]$cat temp.sh
this is my log
this is my log
EOF
[~]$cat temp.sh
this is my log
3.自定义文件描述符:
自定义文件描述符的原理是使用基本的三种文件打开模式
只读(<)
截断(>)
追加(>>)
创建一个文件描述符3,用于打开文件
[~]# exec 3< test.sh
下面就可以直接使用文件描述符打开文件了,但是只能使用一次:
代码如下:
[~]# cat <&3
hello
hello
hello
hello
创建文件描述符4,用于写入文件(可以复用):
代码如下:
[~]# exec 4>test.sh
[~]# echo okok >&4
[~]# cat test.sh
okok
[~]# echo okok >&4
[~]# cat test.sh
okok
这个实际类似前面的:
代码如下:
[~]$cat edit.sql /root/test.sh > temp.sh 2>&1
创建文件描述符5,用于向文件追加内容(也可以复用,不像输入文件描述符那样只能使用1次):
代码如下:
[~]# exec 5>>test.sh
[~]# echo okokok >&5
[~]# cat test.sh
okokok
[~]# echo okokok >&5
[~]# cat test.sh
okokok
okokok
[~]# echo okokok >&5
[~]# cat test.sh
okokok
[~]# echo okokok >&5
[~]# cat test.sh
okokok
okokok