linux shell 之重定向

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章