2014-10-27 71 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. 

是什么复制文件描述符的优点?