Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: virtual files?
- X-seq: zsh-users 21475
- From: "Benjamin R. Haskell" <zsh@xxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: virtual files?
- Date: Fri, 22 Apr 2016 01:26:11 -0400
- Cc: Zsh-Users List <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=benizi-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=Mtvc8myr5YqofrfohPkBEUzIjhfl17W/HOnAzPHzEiM=; b=hMWIqkw6jwzkWMZo1HDmp13K320qrfISpGQFIbxelkYB5lIe/xbtBQYJlSW9Pz2Dvn Ib6gcp4bmNaR1OmV8JJXxVlYh+x+wCAbHF+wJreNl8QIaVtJ5FtjZavZx3ikQGHAPShw bHNZx34F5ZC9xbngL6sbK7dnZnIJlHSTs1EGUyyvtZ1DztLvDz5Ly3vmmdnv37LBhJCV n0ZdXkPH8u/m0MCEFjTuM4WLiWbH43Lq0ToiebgO/SAPgwS96HY42b1sO+JlQWq8kYvp b8ge0w+iEiqcU/R3KK/IDGXJm0gls2m8OVxwXfCt+FDjwgG262zP5WbPcNC7nR9fLXla 45bA==
- In-reply-to: <160420133446.ZM13832@torch.brasslantern.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <8760vdrt5y.fsf@student.uu.se> <CAB2RFrTTibd-8FLs5GoGbMiTS2Gk8L8CVq1gC5im_8LmF3F6Zw@mail.gmail.com> <160420133446.ZM13832@torch.brasslantern.com>
- Sender: benizi@xxxxxxxxxx
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