問題
tt.log文件內容
14 1048576000 /usr/oracle/data/PRD0.2/store_ix10.dbf INDICES
14 367001600 /usr/oracle/data/PRD0.2/store_ix11.dbf INDICES
14 1258291200 /usr/oracle/data/PRD0.3/store_new.dbf STORE_NEW
18 1048576000 /usr/oracle/data/PRD0.2/store_ix10.dbf INDICES
18 367001600 /usr/oracle/data/PRD0.2/store_ix11.dbf INDICES
18 157286400 /usr/oracle/data/PRD0.2/store_ix12.dbf INDICES
我想從tt.log文件中取出1,3列來執行rexec命令,寫了ta.sh執行文件,內容如下:
while read t1 t2 t3 t4
do
echo $t2 $t4
rexec lshas$t1 "ls -lt $t3|awk '{print \$5,\$9}' "
done <tt.log
#bash -x ta.sh
執行順序如下:
+ 0< tt.log
+ read t1 t2 t3 t4
+ echo 1048576000 INDICES
1048576000 INDICES
+ rexec lshas10 ls -lt /usr/oracle/data/PRD0.2/store_ix10.dbf|awk '{print $5,$9}'
1048580096 /usr/oracle/data/PRD0.2/store_ix10.dbf
+ read t1 t2 t3 t4
不讀第二行,請高手指教。
原因:
因為在while循環體中不只有read還有rexec都會從標准輸入(即tt.log)中讀取數據,於是read從標准輸入中讀取了一行內容后,rexec讀取了剩下的所有內容,while也就結束了。那么這個問題怎么解決呢?
1.讓read和rexec分別從不同的輸入中讀取內容,我們知道在shell中標准輸入的文件描述符FD是1,那我們讓tt.log與其它FD(比如3)綁定,然后read就是從&3中讀取數據rexec還是從&1中讀取。在shell中,有一個命令可以實現綁定重定向
綁定輸入重定向
exec 文件描述符[n] <[ file|文件描述符|設備]
綁定輸出重定向
exec 文件描述符[n] >[ file|文件描述符|設備]
關閉綁定重定向
exec n <|> &-
解決方法:
exec 3<tt.log #綁定tt.log到3號fd
while read -u3 t1 t2 t3 t4
do
echo $t2 $t4
rexec lshas$t1 "ls -lt $t3|awk '{print \$5,\$9}' "
done
exec 3<&- #關閉3號fd
#######################################################################
read -u3 i 的意思是從 3 號 fd (file descriptor,文件描述符) 中讀一行數據到 i 變量中
原來的情況是 FD1 ===>read FD1 ===>rexec
修改后的情況是 FD3 ===>read FD1 ===>rexec
