help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] too paranoid?


From: Greg Wooledge
Subject: Re: [Help-bash] too paranoid?
Date: Thu, 10 Mar 2016 11:13:43 -0500
User-agent: Mutt/1.4.2.3i

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.



reply via email to

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