标准文件描述符
Linux系统将每个对象当作文件来处理。Linux用文件描述符来标识每个对象。
最早的三个文件描述符
- STDIN:代表shell的标准输入
- STDOUT:代表shell的标准输出
- STDERR:代表shell的标准错误输出
默认情况下,STDERR文件描述符会和STDOUT文件描述符会指向同样的地方,(that’s to say:错误消息也会输出到显示器输出中)
如何重定向错误?
法一:只重定向错误
将stderr文件描述符(2)放在重定向符号前,则shell会将生成的任何错误发送到指定的重定向文件中。
法二:重定向错误和数据
利用 1> test1 将ls命令本该输出到STDOUT的正常输出重定向到了test1文件。
利用 2> test2 将STDERR的错误消息重定向到了test2文件中。
或者, 也可以将STDERR和STDOUT的输出重定向到同一个输出文件
。此时,我们可以使用 &>
bash shell会自动给错误消息分配较标准输出来说更高的优先级。这样就可以在一处地方查看错误消息了,而不用翻遍整个输出文件。
在脚本中重定向输出
两种方法来在脚本中重定向输出
临时重定向每行输出
永久重定向脚本中的所有命令
可以用exec命令告诉shell在脚本执行期间重定向某个特定文件描述符。$: cat script.sh #!/bin/bash exec 1> testout exec 2 > testout2 echo "this is a test for all output" echo "this is a test for all errors" >&2 $: cat testout this is a test for all output $: cat testout2 this is a test for all errors
在脚本中重定向输入
#!/bin/bash
#这个命令会告诉shell它应该从testfile中获得输入,而不是STDIN。
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line" #输出testfile中每一行的内容
count=$[$count+1]
done
创建自己的重定向 ##
在shell中,最多可以有9个打开的文件描述符。其他6个描述符会从3排到8,可以当作输入或输出重定向。
默认情况下,有三个:
[root@cenos fd]# pwd
/dev/fd
[root@cenos fd]# ls
0 1 2 255
[root@cenos fd]#
创建输出文件描述符
exec 3>testfile
如何从已定向的文件描述符中恢复
#!/bin/bash exec 3>&1 #将STDOUT保存到文件描述符fd3中 exec 1>testout echo "this output to testout file!" exec 1>&3 #将重定向之后的fd1重新指向标准输出(STDOUT) echo "this output to screen!"
创建输入文件描述符
#!/bin/bash exec 6>&0 #将STDIN文件保存到文件描述符fd6中 exec 0< input count=1 while read line do echo "Line $count: $line" count=$[ $count+1 ] done exec 0<&6 #将STDIN恢复到原来的位置(即键盘输入) read -p "Are you done now?" answer case $answer in y) echo "Goodbye";; n) echo "this is end !";; esac
创建读写描述符
$: cat rwFileDes #!/bin/bash # testing input/output file description exec 3<> testfile #用exec命令将文件描述符3分配给文件testfile以进行文件读写 read line <&3 echo "Read: $line" echo "This is a test line" >&3 $: cat testfile first line this is the second line this is third line $: ./rwFileDes $: cat testfile first line This is a test line ine this is third line
关闭文件描述符
exec 3> test17file echo "This is a test line of data" >&3 exec 3>&- #要关闭文件描述符,将它重定向到特殊符号&-
列出打开的文件描述符 ##
lsof命令会列出整个Linux系统打开的所有文件描述符。包括后台运行的所有进程以及登录到系统的任何用户。
shell
/usr/sbin/lsof -a -p $$ -d 0,1,2
阻止命令输出 ##
如果不想显示脚本的输出,可以将STDERR重定向到一个叫做null文件的特殊文件。null文件里什么都没
有,shell输出到null文件的任何数据都不会保存,全部都被丢掉了。
Linux上null文件的位置在/dev/null
创建临时文件 ##
系统上的任何用户账户都有权限读写/tmp目录中的文件。
mktemp命令可以在/tmp目录中创建一个唯一的临时文件。shell会将文件的读和写权限分配给文件的属主,并将你设成文件的属主。
创建本地临时文件
[root@cenos el]# pwd
/home/el
[root@cenos el]# mktemp testing.XXXXXX #mktemp会用6个字符码替换这6个X,保证文件名的唯一
testing.2jVACp
[root@cenos el]# ll
total 4
drwxr-xr-x. 2 root root 4096 Sep 21 08:14 scripts1
-rw-------. 1 root root 0 Sep 21 08:17 testing.2jVACp
在/tmp目录创建临时文件
-t命令会强制mktemp命令在系统的临时目录来创建该文件。
命令会返回文件的全路径名
[root@cenos el]# mktemp -t elsie.XXXXXX
/tmp/elsie.Xa1Of1
创建临时目录
-d: 创建临时目录
[root@cenos elsie]# mktemp -d elsie1.XXXXXX
elsie1.MjdGRl
[root@cenos elsie]# ll
total 8
drwx------. 2 root root 4096 Sep 21 08:27 elsie1.MjdGRl
drwxr-xr-x. 2 root root 4096 Sep 21 08:14 scripts1
记录消息
tee命令:
tee filename
一个T型接头。将从STDIN过来的数据同时发往两处。一处是STDOUT,另一处是tee命令所指定的文件名。
[root@cenos scripts1]# date | tee datefile
Thu Sep 21 08:38:01 CST 2017
[root@cenos scripts1]# cat datefile
Thu Sep 21 08:38:01 CST 2017
tee -a filename: 将数据追加到文件中,用-a选项。
A Example
文件分界符????
[root@cenos scripts2]# cat >output.txt <<EOF
> a
> b
> c
> d
> EOF
[root@cenos scripts2]# cat output.txt
a
b
c
d
[root@cenos scripts2]#
将Hi,first hi,second追加到output.txt中,EOF符号标记了追加到文件中的数据的起止
$: cat catUsage.sh
#!/bin/bash
cat >> output.txt <<EOF
Hi,first
hi,second
EOF
$: cat output.txt
Hi,first
hi,second
$: cat script
#!/bin/bash
while read line
do
echo "this is line: $line"
done < $1
$: ./script output.txt
this is line: Hi,first
this is line: hi,second
http://blog.youkuaiyun.com/feixiaohuijava/article/details/53129413