linux 输入输出重定向

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4743

2013/11/18 01:12


文件描述符         缩写             描述
0                        STDIN          标准输入
1                        STDOUT       标准输出
2                        STDERR        标准错误
~~~~~~~~~~~~~~~~~~~~~~~~~~
重定向错误输出
#ls -al badfile 2> test4
#cat test4
ls: cannot access badfile: No such file or directory
用这种方法shell只会重定向错误信息,而非普通数据。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
重定向错误和正常输出,必须用两个重定向符号。
ls -al test test2 test3 badtest 2> test6 1> test7
test 真是存在
cat test6
[root@localhost ~]# cat test7
-rw-r--r-- 1 root root 22 11-21 09:29 test
root@localhost ~]# cat test6
s: test2: 娌℃湁閭d釜鏂囦欢鎴栫洰褰?
s: test3: 娌℃湁閭d釜鏂囦欢鎴栫洰褰?
s: badtest: 娌℃湁閭d釜鏂囦欢鎴栫洰褰?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果想要STDERR和 STDOUT输出重定向到同一个输出文件。
则可以使用 &>
并且显示出来的错误信息优先级大于普通信息会被优先显示。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
日志中心clean清理根目录
[root@localhost ~]# cat tet
#!/bin/bash
echo "This is an error message" >&2
echo "This is an normal output"
[root@localhost ~]# sh tet
This is an error message
This is an normal output
[root@localhost ~]# sh tet 2> ts
This is an normal output
[root@localhost ~]# cat ts
This is an error message
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@aoi ~]# cat t
#!/bin/bash
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1]
done
[root@aoi ~]# sh t
Line #1: dds
Line #2: faf
Line #3: asfas
Line #4: fsa
Line #5:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
创建输出文件描述符  自定义
[root@aoi ~]# cat r
#!/bin/bash
exec 3>test3out
echo "this should display on the monitor"
echo " and this should be stored in the file" >&3
echo "Then this should be back on the monitor"
[root@aoi ~]# sh r
this should display on the monitor
Then this should be back on the monitor
[root@aoi ~]# cat test3out
and this should be stored in the file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@aoi ~]# cat f
#!/bin/bash
exec 3>&1
exec 1>test4out
echo "This should store in the output file"
echo " a long with this line."

exec 1>&3
echo "Now thins should be back to normal"
[root@aoi ~]# sh f
Now thins should be back to normal
[root@aoi ~]# cat test4out
This should store in the output file
a long with this line.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# cat 5
#!/bin/bash
exec 6<&0
exec 0< t
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now?" answer
case $answer in
Y|y) echo "GOODbey";;
N|n) echo "SOrry,this the end";;
esac
[root@localhost ~]# sh 5
Line #1: dsafdds
Line #2: sfas
Line #3: safdasbv
Are you done now?n
SOrry,this the end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# cat 6
#!/bin/bash
exec 3<> t
read line <&3
echo "Read: $line"
echo "This is a test line" >&3

[root@localhost ~]# cat t
da1
da2
da3
da4
dafasfdsfsd
dasffdasfdasfdas
fasedafsdfasdfsd
[root@localhost ~]# sh 6
Read: da1
[root@localhost ~]# cat t
da1
This is a test line
fsd
dasffdasfdasfdas
fasedafsdfasdfsd
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# cat 7
#!/bin/bash
exec 3> t
echo "This is a  test line of data" >&3
exec 3>&-

cat t
exec 3> t
echo "This'll be bad" >&3
[root@localhost ~]# cat t
da1
This is a test line
fsd
dasffdasfdasfdas
fasedafsdfasdfsd
[root@localhost ~]# sh 7
This is a  test line of data
[root@localhost ~]# cat t
This'll be bad
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# cat 8
#!/bin/bash
exec 3> testfile1
exec 6> testfile2
exec 7< test
lsof -a -p $$ -d0,1,2,3,6,7
[root@localhost ~]# sh 8
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
sh      874 root    0u   CHR  136,0      0t0     2 /dev/pts/0
sh      874 root    1u   CHR  136,0      0t0     2 /dev/pts/0
sh      874 root    2u   CHR  136,0      0t0     2 /dev/pts/0
sh      874 root    3w   REG  253,0        0 65648 /root/testfile1
sh      874 root    6w   REG  253,0        0 65654 /root/testfile2
sh      874 root    7r   REG  253,0       22 65642 /root/tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# cat t
fasdfd
fdasasf
fdsafasasdf
fdasasdfasd
fa
sdfdassf

[root@localhost ~]# cat /dev/null >t
[root@localhost ~]# cat t
将/dev/null作为输入文件,可以快速移除现有文件中的数据而不用先删除文件再创建。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1