1. 输入输出重定向,0-STDIN,1-STDOUT,2-STDERR

ls -al test1 test2 test3 2>error1>normal

2. 可以同时输出ls -al test1 test2 test3 &> all

3. 临时重定向gr.sh: echo 'hello' >&2;echo 'world'

执行。/gr.sh 2> error.txt,则会把错误结果输出到error.txtworld则会输出到屏幕上

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中读取

Exec0<&6#6定向到0

7. 创建读写文件描述符

既可以读又可以写

Exec 3<>testfile

Read line <&3

Echo $line

Echo 'hello world' >&3

8. 关闭文件描述符

Exec3>&-

列出打开的文件描述符

/usr/bin/lsof -a -p $$ -d 0,1,2

9. 阻止命令输出 echo 'hello'>/dev/null

10. 创建临时文件

Mktemptesting.XXXXX(至少三个X,系统随机生成,保证生成的临时文件惟一),返回创建后临时文件名

Mktemp -ttest.XXXXXX (-t 选项会强制在系统的/tmp下创建临时文件,并会返回临时文件的全路径,而不止是文件名)

创建临时目录

tempdir=`mktemp -d dir.XXXXXX`

cd $tempdir

tempfile1=`mktemp temp.XXXXXXX`

tempfile2=`mktemptemp.XXXXXXX`

11. 记录信息:tee

向屏幕输出同时写入文件

Ls -al | teetest.txt

Ls -al | tee-a test.txt (-a选项,表示在文件中追加)

推荐:http://emb.sunplusedu.com/answer/2013/0904/2145.html