1、重定向文件描述符
文件描述符 | 缩写 | 描述 |
0 | STDIN | 标准输入 |
1 | STDOUT | 标准输出 |
2 | STDERR | 标准错误 |
2、永久重定向例子
#!/bin/bash exec 1>testout echo "this is a test of redirecting all output" echo "from a script to another file" echo "without having to redirect every individual line" 运行结果: $ cat testout this is a test of redirecting all output from a script to another file without having to redirect every individual line
在脚本中重定向输入
#!/bin/bash exec 0<testfile count = 1 while read line do echo "Line #$count:$line" count=$[$count +1 ] done
3、在shell中可以有9个可以打开的文件描述符。其他6个文件描述符会从3排到8,并且当作输入或输出重定向都行。
#!/bin/bash exec 3>testoutmyfile echo "hello world" echo "and hello world">&3 echo "ooooo" 运行结果: $ cat testoutmyfile and hello world