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:
for i in *.pdf
s=$(ps -o session= -p $$)
while pgrep -f "$i" --terminal "$t" --uid "$(id -u)" --session "$s" >/dev/null 2>&1
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.