Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: virtual files?



On Wed, Apr 20, 2016 at 4:34 PM, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
wrote:

> On Apr 19, 11:32pm, Benjamin R. Haskell wrote:
> }
> } Using the `=()` substitution ZyX mentions:
> }
> }   () {
> }     local tmp=$1
> }     wget -q $link -O $tmp
> }     echo -n \$
> }     grep \"answer\" $tmp | cut -d \$ -f 2 | cut -d \< -f 1
> }   } =(:)
> }
> } The '() { ... }' construct is an anonymous function, just for controlling
> } the scope of the temporary file, and for passing it in as a positional
> } parameter.  It has the disadvantage that it won't remove the tmp file if
> } something goes wrong.
>
> In fact the point of using =(:) is that it WILL remove the tmp file if
> something goes wrong (unless it's something completely catastrophic like
> the shell itself crashing, but in that case an explicit "rm" wouldn't
> work either).
>

Right, it's the "unless" I'm worried about:

## temp file isn't deleted on exit, so it's still there to be removed
$ ( () { local tmp=$1 ; echo $tmp ; exit 1 } =(:) ) | xargs rm --verbose
removed '/tmp/zsh9OzJL1'

## temp file is deleted with exit trap
$ ( () { local tmp=$1 ; trap "rm $tmp" INT QUIT EXIT ; echo $tmp ; exit 1 }
=(:) ) | xargs rm --verbose
rm: cannot remove '/tmp/zshhzsFQo': No such file or directory


Instead of exit traps, you can use "always" blocks:
>
>   () {
>     local tmp=$1;
>     {
>       : do stuff with $tmp
>       exit
>     } always {
>       rm $tmp
>     }
>   } =(:)
>

Cool!  ~Half my scripts have no need of POSIX-ness, so that'll come in
handy.

-- 
Best,
Ben


Messages sorted by: Reverse Date, Date, Thread, Author