1、標準文件描述符:bash保存了三個文件描述符,分別爲:STDIN標準輸入,STDOUT標準輸出和STDERR標準錯誤,用數字表示分別是0,1,2
標準輸入就比如<符號,鍵盤等,標準輸出和標準錯誤用到的就是顯示屏
如果只想重定向標準錯誤比如:ls -al test 2> test
,會將錯誤信息輸出到test文件中,對於服務器報錯消息很管用。
2、臨時重定向:在重定向到文件描述符時,你必須要在文件描述符數字前加一個&
例如:echo "This is an error message" >&2
這裏子是將這句標準輸出當成標準錯誤
例子:
#!/bin/bash
#
echo "This is an error" >&2
echo "This is normal output"
運行腳本:
This is an error
This is normal output
輸出結果沒有異樣,因爲Linux會將STDERR導向STDOUT,但是你只要在運行腳本的時候重定向了STDERR,那麼標準錯誤就會被重定向
$ bash test > test1
This is normal output
cat test1
This is an error
3、永久重定向:需要用到exec來指定
例子:
#!/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"
例子2:
#!/bin/bash
#
exec 2>testerror
echo "This is the start of the script"
echo "now redirecting all output to another location"
exec 1>testout
echo "This output should go to the testour file"
echo "but this should go to the testerror file" >&2
4、重定向輸入:和上面原理一樣
例子:
#!/bin/bash
#
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
5、創建自己的重定向:因爲文件描述符是0~9,所有還有其他數字可以用,用得最多應該是3,6
例子:
#!/bin/bash
#
exec 3> testout
echo "This should dispaly"
echo "and this should be" >&3
echo "Then this should be"
6、重定向文件描述符:直接看例子,用得比較多,類似於變量的替換
例子:
#!/bin/bash
#
exec 3>&1
exec 1>testout
echo "This should store in"
echo "along with this line."
exec 1>&3
echo "Now things should be back"
(重點在與exec後面的三個文件描述符的重定向)
例子2:
#!/bin/bash
#
exec 6<&0
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now?" answer
case $answer in
Y | y) echo "Goodbay" ;;
N | n) echo "Sorry";;
esac
7、關閉文件描述符:exec 3>&-
,關閉之後腳本就再也不能用此文件描述符,用了就會報錯
例子:
#!/bin/bash
#
exec 3> test17file
echo "This is a test line of data" >&3
exec 3>&-
echo "The won'k work" >&3
錯誤爲:badtest: line 9: 3: Bad file descriptor
8、創建臨時文件和創建臨時目錄:mktemp test.XXXXXX
,創建目錄要用到-d選項,mktemp -d test.XXXXXX
例子:
#!/bin/bash
#
tempdir=$(mktemp -d dir.XXXXXX)
cd $tempdir
tempfile1=$(mktemp temp.XXXXXX)
tempfile2=$(mktemp temp.XXXXXX)
exec 7> $tempfile1
exec 8> $tempfile2
echo "Sending data to directory $tempdir"
echo "This is a test line of data for $tempfile1" >&7
echo "This is a test line of data for $tempfile1" >&8
9、實例:
#!/bin/bash
#
outfile='members.sql'
IFS=','
while read lname fname address city state zip
do
cat >> $outfile << EOF
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('$lname','$fname','$address','$city','$state','$zip');
EOF
done < ${1}
輸入以下數據文件:
cat members.csv
Blum,Richard,123 Main St.,Chicago,IL,60601
Blum,Barbara,123 Main St.,Chicago,IL,60601
Bresnahan,Christine,456 Oak Ave.,Columbus,OH,43201
Bresnahan,Timothy,456 Oak Ave.,Columbus,OH,43201
cat members.sql
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Blum','Richard','123 Main St.','Chicago','IL','60601');
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Blum','Barbara','123 Main St.','Chicago','IL','60601');
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Bresnahan','Christine','456 Oak Ave.','Columbus','OH','43201');
INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Bresnahan','Timothy','456 Oak Ave.','Columbus','OH','43201');