将错误消息重定向到一个文件:

ls noexistfile 2> errorfile

&> 命令生成的所有输出都发送到同一个位置


临时重定向:必须在文件描述符编号前加&

echo "this is an error message" >&2


永久重定向:

使用exec 命令通知shell在脚本执行期间重定向特定的文件描述符:(也可在脚本中间)

exec 1>testout

echo "this"

echo "is"


在脚本中重定向输入:

exec 0< testfile

count=1

while read line

do

   echo "line #$count: $line"

   count=$[$count + 1]

done


创建自己的重定向

exec 3>test3out

echo "this should display on monitor"

echo "this should be stored in the file" >&3

echo "this should display on monitor"


脚本中恢复重定向:

exec 3>&1

exec 1>test4out

echo "this store in the file"


exec 1>&3

echo "now things should be back to normal"


关闭文件描述符:

exec 3>&- 将它重定向到特殊符号&-