bug-bash
[Top][All Lists]
Advanced

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

Re: Design question(s), re: why use of tmp-files or named-pipes(/dev/fd/


From: Bob Proulx
Subject: Re: Design question(s), re: why use of tmp-files or named-pipes(/dev/fd/N) instead of plain pipes?
Date: Mon, 19 Oct 2015 16:01:48 -0600
User-agent: Mutt/1.5.24 (2015-08-30)

Linda Walsh wrote:
> Greg Wooledge wrote:
> >A simple example:
> >diff -u <(sort file1) <(sort file2)
>       You claim <(sort file1) is a filename?

Me too.

> >touch a
> >mv <(sort a) <(sort a)
> mv: cannot move ‘/dev/fd/63’ to ‘/dev/fd/62’: Operation not permitted
> 
> The OS claims that <() generates a file descriptor -- not a filename.

Where does the OS claim it is a file descriptor?  "/dev/fd/63" is a
file name not a file descriptor.  And /dev/fd is a symlink into /proc
space.

  $ ls -ld /dev/fd
  lrwxrwxrwx 1 root root 13 Sep 21 18:19 /dev/fd -> /proc/self/fd

Since it is in /proc it is simply a kernel space construct.  It won't
allow things like rename(2) to work upon it.

I think you were confused by the "fd" part of "/dev/fd" but it is
simply a textual name of the file path.

> It's the same with 'FILENAME'a used with "read < FILENAME >FILENAME"
> 
> >read < FILE > FILE

Unless FILE is a pipe or a tty or other such device then reading and
writing the same file at the same time is a bad idea.  It will get
opened for truncate and empty the file before it can be read.

> >a=<(sort a); chmod +x "$a"

The above is confusing a file named "a" and a variable named "a" which
is always a bad idea.  Better to use different names in experients
such as that so that they don't get confused with each other.

  tmpname=<(sort /dev/null); ls -l "$tmpname"
    ls: cannot access /dev/fd/63: No such file or directory

But as soon as the first commend is finished at the ';' then the
descriptor will be closed and the path will evaporate.  Therefore the
path won't exist for the second command.

But if you use it for the ls command itself then it exists for that
command.

  ls -l <(sort /dev/null)
    lr-x------ 1 rwp rwp 64 Oct 19 15:56 /dev/fd/63 -> pipe:[102059434]

> chmod: cannot access ‘/dev/fd/63’: No such file or directory
> >echo aaa > a
> >read var <(sort a) && echo "var=$var" >a
> ## the above hangs: if <(sort a), was a filename, read would
> ## have read 'aaa' ## from it, and returned immediately.  ## Then the 'echo'
> command would have put 'var=aaa' ## in 'a': instead:

Syntax error in the above.  You probably meant to have 'read' read
from the pipe and not hand it to read as a variable name argument.

  echo read var <(sort a)
    read var /dev/fd/63

You probably meant to use a '<' input redirection there.

  read var < <(sort a) && echo "var=$var" >a
    # equates to: read var < /dev/fd/63

That works as you expected.

Bob



reply via email to

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