Linux标准输入输出及文件描述符

1.标准文件描述符0,1,2

1.1  STDIN标准输入

对终端界面来讲,标准输入时键盘;也能够使用重定向符号<,用重定向指定的文件来代替标准输入文件描述符。linux

#键盘输入
$cat           
This is the first time enter word
This is the first time enter word
This is the second time enter word
This is the second time enter word
#文件输入
$cat < filename
first line
second line

1.2  STDOUT标准输出

在终端界面上,标准输出就是显示器。也能够使用重定向符号>file,将内容输出到文件中;>>file  追加到file文件shell

1.3  STDERR标准错误

默认状况下,标准错误也显示到显示器上。但STDERR不会自动重定向,所以须要设置2>file将错误信息输出到file中。bash

注:能够用 2> file1 1>file2分别将标准错误和标准输出重定向到不一样的文件中this

也能够用 &>file 将标准错误和标准输出重定向到同一文件中,但bash shell会给标准错误分配更高的优先级,先输出标准错误spa

2.在脚本中重定向输出输入

2.1临时重定向

在脚本中重定向到文件描述符时,必须在文件描述符数字以前加&指针

#!/bin/bash
echo "this is an error" >&2
echo "this is normal output"

执行结果
$./shell1.sh           默认状况下Linux会将STDERR定向到STDOUT
this is an error
this is normal output

$./shell.sh 2>shellerr      将错误重定向才能够
this is normal output
$cat shellerr
this is an error

2.2永久重定向 exec

exec命令告诉shell在脚本执行期间重定向某个特定文件描述符:exec 1>testout  将标准输出重定向到testout文件。但对于标准错误,在文件中重定向时也还要指定,如:日志

#!/bin/bash
exec 2>testerror
echo "this is the start"
exec 1>testout
echo "this is normal out"
echo "this is error" >&2     #注:尽管被STDOUT重定向了,仍然能够将指定输出发给STDERR

执行结果:
$./shell2.sh
this is the start
$cat testout
this is normal out
$cat testerror
this is error

2.3 重定向输入exec 0< file

这个 重定向只要在脚本须要输入时,就会做用code

$cat testfile
this is the first line
this is the second line
this is the third line
$cat shell3.sh
#!/bin/bash
exec 0<testfile

while read line
do
  echo "Line: $line"
done
$./shell3.sh
Line: this is the first line
Line: this is the second line
Line: this is the third line

3.建立本身的重定向3~8

3.1建立输出文件描述符orm

exec 3>file     exec 3>>file  追加方式重定向。与标准1,2相似进程

3.2重定向文件描述符

exec 3>&1
exec 1>testout
commond   #重定向到testout文件
exec 1>&3

exec 3>&1 将3重定向到1的位置,也就是STDOUT,表示3的输出都讲显示在屏幕上                 exec 1> testout  将标准输出重定向到testout文件                                                                    exec 1>&3          将标准输出重定向到屏幕

输入与输出相似

exec 6<&0
exec 0<testfile
commond   #重定向到testout文件
exec 0>&6

3.3建立读写文件描述符

exec <>file  命令

当脚本向文件写入数据时,它会从文件指针处的位置开始。会将数据放在指针的当前位置,覆盖该位置的原来的数据

#!/bin/bash
exec 3<>testfile

read line <&3
echo “Read: $line"
echo "this is error"
执行结果
$cat testfile
this is the first line
this is the second line
this is the third line

$./shell4.sh
Read: this is the first line

$cat testfile
this is the first line
this is error
this is the third line

3.4关闭文件描述符  

exec 3>&-   改语句会关闭文件描述符3,从而阻止在脚本中使用它,若开始exec 3>file ,一旦关闭就不能够再向file写数据,不然报错。注:若在后面脚本中再次启用exec 3>file,与file相同文件,则shell会用新文件代替已有文件。

4.列出打开的文件描述符 lsof

lsof命令会列出整个linux系统打开的全部文件描述符,选项-p,-d,-a经常使用来过滤输出               -p 指定进程ID(-p $$ 显示当前进程ID);-d 指定要显示的文件描述符。

5.阻止命令输出null

/dev/null  Linux系统上null文件的标准位置,重定向到改文件的任何数据都会被丢掉。不会显示。能够将 STDERR重定向到这个文件

cat  /dev/null  > logfile   清除日志的通用方法,不删除文件,只将文件内容清空。

6.建立临时文件

Linux使用/tmp目录来存放不须要一直保留的文件,大多数Linux配置了,在启动时,自动删除/tmp目录的全部文件。mktemp建立临时文件,不用默认的umask的值,属主有读写权。

建立临时文件和目录mktemp

使用模板能够建立不一样名字的多个文件,在文件末尾加上6个XXXXXX就能够了。mktemp命令会用6个字符码替换6个X,从而保证目录名在文件中是惟一的。

caishu@lab403-1F:~$ mktemp test.XXXXXX
test.h47XzR
caishu@lab403-1F:~$ mktemp test.XXXXXX
test.wLBSRM
caishu@lab403-1F:~$ mktemp test.XXXXXX
test.gjtA5O
caishu@lab403-1F:~$ ls -al test*
-rw------- 1 caishu caishu    0  5月 16 22:39 test.gjtA5O
-rw------- 1 caishu caishu    0  5月 16 22:39 test.h47XzR
-rw------- 1 caishu caishu    0  5月 16 22:39 test.wLBSRM

mktemp -t 命令在/tmp目录建立文件,返回的文件带有全路径,能够在linux系统上任何目录下引用该临时文件。

mktemp -d  建立临时目录     也能够用模板 mktemp -d dir.XXXXXX

7.记录消息

用tee命令能够一边发送到显示器一边发送到文件

$date | tee file1
mondey may 16 23:03
$cat file1
mondey may 16 23:03

能够用tee -a file追加到文件

相关文章
相关标签/搜索