[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem with open and rm
From: |
Micah Cowan |
Subject: |
Re: Problem with open and rm |
Date: |
Wed, 16 Mar 2011 11:42:17 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 |
(03/16/2011 03:54 AM), Barrie Stott wrote:
> The script that follows is a cut down version of one that came from elsewhere.
>
> #!/bin/bash
>
> cp /tmp/x.html /tmp/$$.html
> ls /tmp/$$.html
> [ "$DISPLAY" ] && open /tmp/$$.html
> ls /tmp/$$.html
> rm -f /tmp/$$.html
>
> I'm on an Imac with OS X 10.6.6. If I run the script as it stands, the open
> tries to open /tmp/$$.html in a new tab in the Safari browser and fails with
> the message: No file exists at the address “/tmp/13551.html”. The terminal
> output is the following pair of lines, which suggest that the file is still
> around after open failed:
>
> /tmp/13551.html
> /tmp/13551.html
>
> If I comment out the final 'rm' line and run it again, I get what I want
> displayed (and a similar pair of lines on the terminal) but unfortunately the
> temporary file is left lying around.
>
> My two questions are:
> Why?
>From the symptoms you describe, it seems extremly likely that the "open"
command simply sends a message to Safari and exits. This exit may very
well occur before Safari actually gets a chance to obey the message.
This then produces a race: will Safari open the file before bash gets to
and executes the "rm" command, or...?
> How can I change the script so that I can both view the file and have it
> removed?
That depends on Safari, really. There's no way for a script to know
whether Safari has opened the tab, unless Safari provides a means for
knowing it. Perhaps open has an option that lets it block until Safari
acknowledges having opened the file?
If not, then probably the best you can do is a heuristic: sleep a couple
seconds after evaluating "open", and then continue on.
--
Good luck!
Micah J. Cowan
http://micah.cowan.name/