Abra o arquivo se ainda não estiver aberto com o script .sh

1

Eu tenho esse script que abre um arquivo .html , mas quero alterá-lo para apenas abri-lo se ele ainda não estiver aberto.
Alguém sabe algumas linhas que precisam ser adicionadas para conseguir isso?

#!/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.

    
por Cold_Class 10.06.2018 / 11:33

1 resposta

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.

Você pode usar lockfile do procmail package . Como o seu man page contém um exemplo que corresponde exatamente ao seu caso de uso, eu vou citar:

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.

Por padrão, todos os outros scripts aguardarão o desbloqueio do arquivo, testando a cada oito segundos por padrão (altere com, por exemplo, lockfile -5 important.lock por cinco segundos). Se você quiser apenas verificar se está bloqueado, use o comando clássico test :

if [ -e important.lock ]; then
  # stuff to do if locked
else
  # stuff to do if not locked
fi
    
por dessert 10.06.2018 / 12:13