0

To count open epubs I used this:

# - determine how many epubs are open -
NUMBER_OF_OPEN_EPUBS=0
while read -r LINE ; do
    if [ "$(echo $LINE | rev | cut -c1-5 | rev)" = ".epub" ]; then
        NUMBER_OF_OPEN_EPUBS="$(($NUMBER_OF_OPEN_EPUBS+1))"
    fi
done < <(lsof | grep "\.epub")
# --- end determine how many epubs are open ---

and it always worked. But I wanted to extend it to fb2 files (similar to epubs) as well so I got an fb2 for testing and couldn't make it work. To illustrate the underlying problem in it's simplest form:

With 2 files, /test.epub & /test.fb2 open in fbreader in seperate windows, in bash, in lxterminal, under Ubuntu 14.04 LTS and plain Openbox:

me@nu:~$ lsof | grep "\.fb2" | tr -s " "
me@nu:~$ lsof | grep "\.epub" | tr -s " "
fbreader 28982 me 12r REG 8,5 346340 8375 /test.epub
me@nu:~$

Why doesn't lsof see the fb2? In practical terms, I suppose I could use ps, which exhibits no prejudice against fb2 files (and incidentally proves grep isn't to blame) instead, but why does lsof snub fb2 files?

================== P.S. I edited this to put it in proper context here, even though, thanks to Mr.Hyde, it is solved already. The question as stated reflects an implied and unexamined assumption which turns out to be false. See answer.

  • 1
    Did you mean `/test.fb2` instead of `/test.fbw`? – Steven Jan 04 '16 at 04:52
  • Yes. Oohhhh. Took me a few looks to catch where you meant. Lesse if I can figure out how to edit that. – Lew Rockwell Fan Jan 04 '16 at 05:58
  • @Steven. Indeed, thanks for catching that. Can I give you brownie points for that somehow? I corrected that and one grammar error also. Somebody had suggested highlighting the file and program names, just as you did in your comment, so I accepted that though I'm curious as to why that is the custom. – Lew Rockwell Fan Jan 04 '16 at 06:17
  • 2
    This is probably because of how your application handles files. That is, it doesn't keep the other file open. – hyde Jan 04 '16 at 06:31
  • However, this doesn't really look like programming question, so is off-topic here. If it is about programming, perhaps you could explain what you are trying to do. Otherwise, some other SE site would be better place to ask. – hyde Jan 04 '16 at 06:35
  • Actually it is, but I tried to boil it down to the simplest case to illustrate the puzzle. Your suggestion that fbreader may not actually handle epubs and fb2s in the same way is plausible and hadn't occured to me. If that is it, ps is what I need to use instead. Anyway the larger project is a bookmarking script that improves on what fbreader has built in. At one point I use the output of lsof to determine how many epubs are open. Now I'm trying to extend it to more formats and as a first step, to determine the total number of epubs + fb2s open. I have a devil of time pasting here & i'm out of – Lew Rockwell Fan Jan 04 '16 at 07:31

1 Answers1

1

Hyde's comment was the clue I needed. So he should get the credit. I'm not clear how that works yet, but I've lurked this site enough to know it is important to y'all.

So, right, if fbreader keeps one file open but not the other, as Hyde suggested, the question would be why. I was assuming the file type was the important thing, but once I looked at it that way the possibility was obvious and I tested it and the issue isn't type but file size. I've only found one fb2 to test my script with and it happens to be smaller than most of my epubs. I found a small epub and it behaved the same way. Presumably if the file is small enough fbreader just stores the whole thing in memeory but can't for a larger file. So, mea culpa, the problem as stated is spurious. Bottom line from a scripting pov is I need to use ps -eo args instead of lsof because ps sees the file as an argument to a command that started an open process rather than an open file and that is probably more to the point anyway. Thanks, gentles.