help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] too paranoid?


From: John McKown
Subject: Re: [Help-bash] too paranoid?
Date: Thu, 10 Mar 2016 10:33:15 -0600

I appreciate the information. I was afraid that my tests were too
simplistic. What triggered this was my starting to learn the GO language,
which has a "defer" command. You can "defer" any number of commands and
they will execute, in LIFO order, when the function in which they were
executed ends, however that is: panic, return, or "fall off the end".
Somewhat akin, conceptually, to thethe C language "on_exit()" or "atexit()"
ability. I was trying for something similar in a script.

On Thu, Mar 10, 2016 at 10:13 AM, Greg Wooledge <address@hidden> wrote:

> On Thu, Mar 10, 2016 at 09:43:48AM -0600, John McKown wrote:
> > temp1=$(mktemp) #create temporary file & get name
> > exec 11>${temp1} # make /dev/fd/11 point to said file
> > rm ${temp1} #unlink it, but it remains due to the above exec
>
> Quote the parameter expansion.  Also, you have to open the file for
> both reading and writing.
>
> exec 11<>"$temp1" && rm "$temp1"
>
> > temp1='/dev/fd/10' #and now use it nicely
>
> 10 or 11?
>
> Also... I seem to remember there is some incredibly surprising and arcane
> change of behavior when you do this, but I don't recall the details.
> It would be a lot simpler to skip the /dev/fd/ nonsense and just use the
> FD as an FD.
>
> But the problem with that is there's no way to tell bash to rewind the
> file so you can get the information back OUT of it.
>
> imadev:~$ exec 11<>/tmp/greg.11
> imadev:~$ rm /tmp/greg.11
> imadev:~$ echo "some data" >&11
> imadev:~$ cat <&11
> imadev:~$ echo "some more data" >&11
> imadev:~$ read <&11
> imadev:~$ declare -p REPLY
> declare -- REPLY=""
> imadev:~$ cat <&11
> imadev:~$ exec 11>&-
> imadev:~$ cat <&11
> bash: 11: Bad file number
>
> Oh!  Yes.  Here's the surprising behavior.  Or at least *one* surprising
> behavior.  It requires that Linux /dev/fd/ crap.
>
> address@hidden:~$ exec 11<>/tmp/greg.11
> address@hidden:~$ rm /tmp/greg.11
> address@hidden:~$ echo "some data" >&11
> address@hidden:~$ cat <&11
> address@hidden:~$ cat /dev/fd/11
> some data
>
> Huh?!?
>
> address@hidden:~$ echo "more data" >&11
> address@hidden:~$ cat /dev/fd/11
> some data
> more data
> address@hidden:~$ cat <&11
> address@hidden:~$ echo "new data" >/dev/fd/11
> address@hidden:~$ cat <&11
> address@hidden:~$ cat /dev/fd/11
> new data
>
> I give up trying to understand.  If bash had a rewind command, you could
> just use the FD easily, portably, and without incomprehensible trickery.
>



-- 
A fail-safe circuit will destroy others. -- Klipstein

Maranatha! <><
John McKown


reply via email to

[Prev in Thread] Current Thread [Next in Thread]