Kuhl said:
Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check? Thanks.
The result of this check is absolutely worthless: before you use the
result of the check, the closed file may be opened, or the open file may
be closed. The file may even cease to exist before you do anything.
Besides: vim (VI iMproved) opens the file, reads it, then closes it
again, then you work on some copy (start editing a file, then, on
another tty look at the editor's open files in /proc/<pidofeditor>/fd).
When you save the result, vi may very create a new file rather than
rewrite the old file (ls -lisa <somefile>; vi <somefile> # and change
it, then save it; ls -lisa <somefile> # the first number is the inode).
It's a similar issue with trying to use the existence of a file as a
locking mechanism: perldoc -q file.lock
Josef