2014-10-27 69 views
1

您好我有一個的投擲一個錯誤的bash腳本:bash的語法錯誤 「EXEC 11 <<& - 」

scriptx.sh:行276:附近意外的標記'&「

錯誤語法錯誤關於這個片段的最後一行:

find * -type f > $loopfile 
exec 11<$loopfile 
while read file; do 
    # Here some process.... 
    : 
done 
exec 11<<&- 

是什麼目的:

exec 11<$loopfile 
exec 11<<&- 

ŧ漢克斯。

回答

2

bash手冊頁的兩個部分與此處相關。

Redirecting Input

Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.

The general format for redirecting input is: 

      [n]<word 

Duplicating File Descriptors

The redirection operator

[n]<&word 

is used to duplicate input file descriptors. If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.

所以第一線exec 11<$loopfile打開了文件描述符11打開讀取輸入和輸入設置爲來自$loopfile

第二行exec 11<<&-然後關閉(由第一行打開的)描述符11 ......或者說,它不是因爲chepner注意到我在初讀時忽略的語法錯誤。正確的行應該是exec 11<&-關閉fd。

要回答在OP的自我回答中詢問的後續問題,除非此腳本在該循環中使用fd 11,否則這些行似乎沒有用處。我通常會認爲這將在read的循環中使用,但這需要-u 11(並且可以使用while read file; do ... done <$loopfile輕鬆完成)。

+1

這應該是'exec 11 <& - '關閉描述符11;兩個<< <<將是一個語法錯誤。 – chepner 2014-10-27 14:56:28

+0

@chepner好點。我錯過了OP中的錯誤問題,並專注於「這個問題是什麼」。我會更新。 – 2014-10-27 14:59:44

0

錯誤被拋出,因爲關閉一個文件描述符只需要一個重定向操作11<&-和腳本有兩個:11<<&-

關於如何使用它的代碼示例:

exec 11<$loopfile # File descriptor 11 is made copy of $loopfile 
while read -u 11 file; do 
    : # process 
done 
exec 11<&-   # File descriptor 11 is closed. 

是什麼複製文件描述符的優點?