1

I got this script that opens an .html file, but I want to change it to only opening it if it isn't open yet.
Does anyone know some lines that have to be added in order to achieve this?

#!/bin/bash
fileFragment=$"*project/html/index.html"
fullPath=$(find ../ -path $fileFragment)
xdg-open $fullPath &

EDIT
I would like a generic solution which is why I kept the question so simple, it's not about browsers, it's about any file I opened using xdg-open, I already specified it by providing this example, I don't want to specify it anymore to specific browsers.
But if there is no easy generic solution then I'd rather have an answer for my specific case than none: So in my case I'm using firefox.

1 Answers1

1

The following solution only works for commands that keep running until you close the program/tab. Unfortunately that’s not the case for most GUI programs and xdg-open. In the case of Firefox one could get a list of URLs currently opened and grep it, but that’s a neither generic nor ideal solution. As for testing whether a program is running, see this question.


You can use lockfile from the procmail package Install procmail. As its man page holds an example exactly matching your use case, I’ll just quote it:

Suppose you want to make sure that access to the file "important" is serialised, i.e., no more than one program or shell script should be allowed to access it. For simplicity's sake, let's suppose that it is a shell script. In this case you could solve it like this:

...
lockfile important.lock
...             
access_"important"_to_your_hearts_content
...
rm -f important.lock
...

Now if all the scripts that access "important" follow this guideline, you will be assured that at most one script will be executing between the lockfile and the rm commands.

By default, every other script will wait for the file to be unlocked again, testing every eight seconds by default (change with e.g. lockfile -5 important.lock for five seconds). If you just want to check whether it’s locked, you can use the classic test command:

if [ -e important.lock ]; then
  # stuff to do if locked
else
  # stuff to do if not locked
fi
dessert
  • 39,982
  • Could you complete your example by adding my existing script? I'm an absolute beginner on Ubuntu scripts. I do not know what to put in place of the 4 ... parts - and how do I 'use' lockfile from procmail? Do you mean I need to sudo apt-get install procmail before being able to use that script? – Cold_Class Jun 10 '18 at 11:07
  • I think my question is very easy to understand but I added some more info for ya, I want NOTHING to happen when I execute this script and there is ANY browser window/tab/whatever open with that file. If it's too complex I'm happy with any easy solutions that still make it work like checking if a browser is running at all – Cold_Class Jun 10 '18 at 12:09
  • Thank you, for now I am using the solution from an answer to the question you linked! I don't know how to use URL-Lists script in my example. – Cold_Class Jun 11 '18 at 21:40