Linux输入输出重定向


Linux输入输出重定向
 
输入输出重定向,0-STDIN,1-STDOUT,2-STDERR
ls -al test1 test2 test3 2>error 1>normal
可以同时输出ls -al test1 test2 test3 &> all
       3.  临时重定向gr.sh: echo ‘hello’  >&2;echo ‘world’
 
执行./gr.sh 2> error.txt,则会把错误结果输出到error.txt而world则会输出到屏幕上
4.  永久重定向:exec
 
exec 1>test.txt则下面的echo语句输出都会指向test.txt,一旦重定向,就无法轻易重定向加原来的位置,后面进行讲解
输入重定向:exec 0< test.txt,将会从test.file中读取
While read line
Do
Echo $line
Done
创建自己永久的重定向 (大于等于3)
Exec 3> grself.txt
Echo ‘hello world’ >&3
5.  重定向文件描述符
 
Exec 3>&1  #把3定向到1
Exec 1>test.txt  #把1定向到test.txt文件
Echo ‘helloworld’ >&1
Exec 1>&3#把1定向到3,即还原到最初状态
6.  输入文件描述符
 
Exec 6<&0#把0定向到6
Exec 0<testfile #把testfile定向到0
Read line#从testfile中读取
Exec 0<&6#把6定向到0
7.  创建读写文件描述符
 
既可以读又可以写
Exec 3<>testfile
Read line <&3
Echo $line
Echo ‘hello world’ >&3
8.  关闭文件描述符
 
Exec 3>&-
列出打开的文件描述符
/usr/bin/lsof -a -p $$ -d 0,1,2
9.  阻止命令输出 echo ‘hello’>/dev/null
 
10.  创建临时文件
 
Mktemp testing.XXXXX(至少三个X,系统随机生成,保证生成的临时文件惟一),返回创建后临时文件名
Mktemp -t test.XXXXXX (-t 选项会强制在系统的/tmp下创建临时文件,并会返回临时文件的全路径,而不止是文件名)
创建临时目录
tempdir=`mktemp -d dir.XXXXXX`
cd $tempdir
tempfile1=`mktemp temp.XXXXXXX`
tempfile2=`mktemp temp.XXXXXXX`
11.  记录信息:tee
 
向屏幕输出同时写入文件
Ls -al | tee test.txt
Ls -al | tee -a test.txt (-a选项,表示在文件中追加)

相关内容

    暂无相关文章