2

I have a lot of documents to view, and I'd like to view them one by one, so the next opens when I close the previous.

I've done this with evince before, with

for i in `ls | grep .pdf`; do evince $i; done

However doing the same with xdg-open fails because xdg-open does not block like evince does.

Is there a way to run the same for loop, but with xdg-open, so that when I close the application that was opened, the next iteration of the for loop runs?

  • short answer: no. long answer: modify the source – Ipor Sircer Mar 5 '18 at 16:32
2

I couldn't find a clean way, so this is a work-around; open each file then run a busy-loop waiting for "the" matching process to exit. I've also updated your for loop so that you're not parsing ls and are quoting the filename parameter:

#!/bin/bash

for i in *.pdf
do
  xdg-open "$i"
  t=$(tty)
  t=${t##/dev/}
  s=$(ps -o session= -p $$)
  while pgrep -f "$i" --terminal "$t" --uid "$(id -u)" --session "$s" >/dev/null 2>&1
  do
    sleep 1
  done
done

The assumption here is that xdg-open will open the file; that process gets forked off by the desktop environment and control returns to the script. The script then gathers the tty, session, and current user ID and asks pgrep to look for (the) process matching all of these criteria:

  • full process name includes the filename from the loop
  • the associated terminal is the one we're running from
  • the UID of the process matches ours
  • the process session matches ours

... all in an attempt to catch only the corresponding process that xdg-open launched.

When that process no longer exists, we continue with the for loop on to the next file.

If the one-second delay is too long, you could replace that (on Linux) with a sub-second sleep, or a simple : for no waiting at all.

  • This is an awesome solution! A little hacky but it gets the job done. Very smart. – Brydon Gibson Mar 5 '18 at 16:40
  • Thank you! Don't feel rushed to accept my (or any first) answer; I'm not an expert at desktop environments, so there may be a better way. – Jeff Schaller Mar 5 '18 at 16:42
  • Unfortunately this will fail if the file is opened by an existing process. You can see this happen e.g. with gedit, if that’s the default editor for text files: start gedit, then xdg-open a text file; the file will open in gedit, and the above script won’t match anything. – Stephen Kitt Mar 6 '18 at 12:21
  • @StephenKitt would the gedit process have to have started from the same tty/session, too? – Jeff Schaller Mar 6 '18 at 12:32
  • That’s not sufficient, because the file name doesn’t show up on the command line of the running process. – Stephen Kitt Mar 6 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.