什么200 GT;" $ somefile"完成? [英] What does 200>"$somefile" accomplish?
问题描述
我发现样板羊群(1)
code 这看起来很有希望。现在我想盲目使用它之前了解的组件。
好像这些功能是使用羊群
涌向[-sxun] [-w超时] FD
第三种形式是shell脚本里面方便,通常使用
方式如下:
块引用>(
涌向-s 200
#...命令下执行锁...
)200 GT;的/ var /锁/ mylockfile这件作品,我失去了(从样品包装函数的)是这个符号
评估EXEC $ LOCKFD> \\$锁定文件\\
或从
羊群
速记手册页200 GT;的/ var /锁/ mylockfile
这是什么目的?
我通知
羊群
通过一个比最初的重定向事业以外的值羊群
抱怨后续命令羊群:50:坏的文件描述符
看起来
羊群
使用文件描述符作为地图就知道要操作的文件。为了这工作,虽然,这些描述符将不得不仍然各地和相关的文件,对吧?重定向完成后,并且在创建锁定文件,不是文件关闭,并且与打开文件关联的文件描述符汽化?我以为文件描述符只用打开文件有关。
这是怎么回事?
解决方案200 GT;的/ var /锁/ mylockfile
这将创建一个文件
的/ var /锁/ mylockfile
可通过文件描述符200写入子壳体内。数字200是一个任意之一。采摘高数量减少的任何子壳注意到额外的文件描述符内部的命令的机会。(通常,文件描述符0,1和2通过标准输入,stdout和stderr分别使用。这个数字可能是低达3。)
羊群-s 200
然后
羊群
用于锁定通过pviously创建文件描述符$ P $文件。它需要写访问该文件,其中>在
提供。请注意,这种情况发生的之后的上面的重定向。200 GT;
I've found boilerplate
flock(1)
code which looks promising. Now I want to understand the components before blindly using it.Seems like these functions are using the third form of
flock
flock [-sxun] [-w timeout] fd
The third form is convenient inside shell scripts, and is usually used the following manner:
( flock -s 200 # ... commands executed under lock ... ) 200>/var/lock/mylockfile
The piece I'm lost on (from the sample wrapper functions) is this notation
eval "exec $LOCKFD>\"$LOCKFILE\""
or in shorthand from the
flock
manpage200>/var/lock/mylockfile
What does that accomplish?
I notice subsequent commands to
flock
passed a value other than the one in the initial redirect causeflock
to complainflock: 50: Bad file descriptor
It seems like
flock
is using the file descriptors as a map to know which file to operate on. In order for that to work though, those descriptors would have to still be around and associated with the file, right?After the redirect is finished, and the lock file is created, isn't the file closed, and file descriptors associated with the open file vaporized? I thought file descriptors were only associated with open files.
What's going on here?
解决方案200>/var/lock/mylockfile
This creates a file
/var/lock/mylockfile
which can be written to via file descriptor 200 inside the sub-shell. The number 200 is an arbitrary one. Picking a high number reduces the chance of any of the commands inside the sub-shell "noticing" the extra file descriptor.(Typically, file descriptors 0, 1, and 2 are used by stdin, stdout, and stderr, respectively. This number could have been as low as 3.)
flock -s 200
Then
flock
is used to lock the file via the previously created file descriptor. It needs write access to the file, which the>
in200>
provided. Note that this happens after the redirection above.这篇关于什么200 GT;" $ somefile"完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!