Discussion:
Bash heredoc on FD 3
(too old to reply)
Steven Penny
2018-12-02 18:43:17 UTC
Permalink
cyg Simple
2018-12-03 15:43:31 UTC
Permalink
   $ cat hello.sh
   awk -f /dev/fd/3 3<<eof
   BEGIN {
     print "hello world"
   }
   eof
   $ dash hello.sh
   hello world
   $ bash hello.sh
   awk: fatal: can't open source file `/dev/fd/3' for reading (No such
file or
   directory)
I tried also with Debian and both Dash and Bash work as expected. What is
causing Cygwin Bash to fail here?
Same for me and interestingly:

$ ls -ld /dev/fd/*
ls: cannot access '/dev/fd/3': No such file or directory
ls: cannot access '/dev/fd/31': No such file or directory
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:39 /dev/fd/0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:39 /dev/fd/1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:39 /dev/fd/2 -> /dev/pty2
--
cyg Simple

--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
cyg Simple
2018-12-03 15:55:57 UTC
Permalink
Post by cyg Simple
    $ cat hello.sh
    awk -f /dev/fd/3 3<<eof
    BEGIN {
      print "hello world"
    }
    eof
    $ dash hello.sh
    hello world
    $ bash hello.sh
    awk: fatal: can't open source file `/dev/fd/3' for reading (No
such file or
    directory)
I tried also with Debian and both Dash and Bash work as expected. What is
causing Cygwin Bash to fail here?
$ ls -ld /dev/fd/*
ls: cannot access '/dev/fd/3': No such file or directory
ls: cannot access '/dev/fd/31': No such file or directory
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec  3 10:39 /dev/fd/0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec  3 10:39 /dev/fd/1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec  3 10:39 /dev/fd/2 -> /dev/pty2
And:

$ dash -c '/bin/ls -l /dev/fd/'
total 0
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 2 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 3 -> /proc/27356/fd

$ dash -c '/bin/ls -l /dev/fd/'
total 0
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 2 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:51 3 -> /proc/18740/fd


So in Bash the process is no longer available. Adding -d to the above:

$ dash -c '/bin/ls -ld /dev/fd/*'
/bin/ls: cannot access '/dev/fd/3': No such file or directory
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:50 /dev/fd/0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:50 /dev/fd/1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:50 /dev/fd/2 -> /dev/pty2
--
cyg Simple

--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Eliot Moss
2018-12-03 17:28:18 UTC
Permalink
Post by cyg Simple
$ ls -ld /dev/fd/*
ls: cannot access '/dev/fd/3': No such file or directory
ls: cannot access '/dev/fd/31': No such file or directory
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec  3 10:39 /dev/fd/0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec  3 10:39 /dev/fd/1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec  3 10:39 /dev/fd/2 -> /dev/pty2
Every process gets a standard input, output, and error file descriptor.
Other exist only if they're opened. These are process-specific.

What's mysterious about the reported case is that the 3<< ... did not
seem to create a /dev/fd/3, at least not at the right "time" ...

Regards - Eliot Moss

--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Houder
2018-12-04 12:13:31 UTC
Permalink
$ cat hello.sh
awk -f /dev/fd/3 3<<eof
BEGIN {
print "hello world"
}
eof
$ dash hello.sh
hello world
$ bash hello.sh
awk: fatal: can't open source file `/dev/fd/3' for reading (No such file or
directory)
I tried also with Debian and both Dash and Bash work as expected. What is
causing Cygwin Bash to fail here?
File to which symlnk /dev/fd/3 refers has "gone"; different from Linux, where
the file is "deleted", but still "available".
(note: dash uses a different implementation)

I used fd 7 in testing. Below the output on Cygwin ... Observe the difference
when executed on Linux.

Henri

hello.sh:

#cat 7<<\EOF 0<&7 # works on both Linux and Cygwin
#cat 7<<\EOF 0<&7 /dev/fd/7 # fails on Cygwin
#ls 7<<\EOF 0<&7 -l /proc/self/fd /tmp /dev/fd/7
ls 7<<\EOF 0<&7 -lL /proc/self/fd /tmp /dev/fd/7
Hello world!
EOF
exit

# Using: ls 7<<\EOF 0<&7 -l /proc/self/fd /tmp /dev/fd/7

64-@@ bash hello.sh
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 /dev/fd/7 -> /tmp/sh-thd.3PSuc2

/proc/self/fd:
total 0
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 0 -> /tmp/sh-thd.3PSuc2
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 1 -> /dev/pty1
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 2 -> /dev/pty1
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 3 -> /proc/4972/fd
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 7 -> /tmp/sh-thd.3PSuc2

/tmp:
..

64-@@ dash hello.sh
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 /dev/fd/7 -> pipe:[4294969740]

/proc/self/fd:
total 0
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 0 -> pipe:[4294969740]
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 1 -> /dev/pty1
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 2 -> /dev/pty1
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 3 -> /proc/3740/fd
lrwxrwxrwx 1 Henri None 0 Dec 4 12:37 7 -> pipe:[4294969740]

/tmp:
..

# Using: ls 7<<\EOF 0<&7 -lL /proc/self/fd /tmp /dev/fd/7

64-@@ bash hello.sh
ls: cannot access '/dev/fd/7': No such file or directory
/proc/self/fd:
ls: cannot access '/proc/self/fd/0': No such file or directory
ls: cannot access '/proc/self/fd/7': No such file or directory
total 0
l????????? ? ? ? ? ? 0
crw--w---- 1 Henri None 136, 1 Dec 4 12:39 1
crw--w---- 1 Henri None 136, 1 Dec 4 12:39 2
dr-xr-xr-x 2 Henri None 0 Dec 4 12:39 3
l????????? ? ? ? ? ? 7

/tmp:
..

64-@@ dash hello.sh
prw------- 1 Henri None 0 Dec 4 12:40 /dev/fd/7

/proc/self/fd:
total 0
prw------- 1 Henri None 0 Dec 4 12:40 0
crw--w---- 1 Henri None 136, 1 Dec 4 12:40 1
crw--w---- 1 Henri None 136, 1 Dec 4 12:40 2
dr-xr-xr-x 2 Henri None 0 Dec 4 12:40 3
prw------- 1 Henri None 0 Dec 4 12:40 7

/tmp:
..

=====


--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
cyg Simple
2018-12-04 14:21:55 UTC
Permalink
Post by Houder
$ cat hello.sh
awk -f /dev/fd/3 3<<eof
BEGIN {
print "hello world"
}
eof
$ dash hello.sh
hello world
$ bash hello.sh
awk: fatal: can't open source file `/dev/fd/3' for reading (No such file or
directory)
I tried also with Debian and both Dash and Bash work as expected. What is
causing Cygwin Bash to fail here?
File to which symlnk /dev/fd/3 refers has "gone"; different from Linux, where
the file is "deleted", but still "available".
(note: dash uses a different implementation)
Dash is faster in processing the data but can be made to fail if you add
-d to the ls commands you're using.
--
cyg Simple

--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Houder
2018-12-05 06:55:08 UTC
Permalink
Post by cyg Simple
Dash is faster in processing the data but can be made to fail if you
add -d to the ls commands you're using.
?

Oh, I believe you refer to:

https://cygwin.com/ml/cygwin/2018-12/msg00035.html
( Re: Bash heredoc on FD 3 )
Post by cyg Simple
$ dash -c '/bin/ls -ld /dev/fd/*'
/bin/ls: cannot access '/dev/fd/3': No such file or directory
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:50 /dev/fd/0 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:50 /dev/fd/1 -> /dev/pty2
lrwxrwxrwx 1 eboyd53 eboyd53 0 Dec 3 10:50 /dev/fd/2 -> /dev/pty2
uhm, the facts are slightly different ...

/dev/fd is, well, a "moving target", it is symlnk to /proc/self/fd.

self refers to the "current process" ... it changes each time.

/proc/self/fd shows the open fd's of the current process ...

Currently, I am on Linux (Fedora) ...

@@ # currently I am executing the shell (bash)
@@ # self points to the procid of my bash ...

@@ printf "%s\n" /dev/fd/*
/dev/fd/0
/dev/fd/1
/dev/fd/10
/dev/fd/11
/dev/fd/2
/dev/fd/255
/dev/fd/3

@@ # however ls has NO open fd 3 if I invoke it like this:
@@ # (self points to the procid of my ls command)

@@ ls -ld /dev/fd/* # /dev/fd/* expands to the list above (bash)
ls: cannot access '/dev/fd/255': No such file or directory
ls: cannot access '/dev/fd/3': No such file or directory
lrwx------ 1 henri henri 64 Dec 5 07:26 /dev/fd/0 -> /dev/pts/0
lrwx------ 1 henri henri 64 Dec 5 07:26 /dev/fd/1 -> /dev/pts/0
lrwx------ 1 henri henri 64 Dec 5 07:26 /dev/fd/10 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:26 /dev/fd/11 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:26 /dev/fd/2 -> /dev/pts/0

@@ # ls HAS an open fd 3 if I invoke it like this:
@@ (# use strace to see it happen -- ls opens the directory)

@@ ls -lL /dev/fd # /dev/fd refers to /proc/self/fd
total 0
crw------- 1 henri tty 136, 0 Dec 5 07:38 0
crw------- 1 henri tty 136, 0 Dec 5 07:38 1
crw-rw----+ 1 root video 226, 0 Dec 5 07:01 10
crw-rw----+ 1 root video 226, 0 Dec 5 07:01 11
crw------- 1 henri tty 136, 0 Dec 5 07:38 2
dr-x------ 2 henri henri 0 Dec 5 07:38 3
@@ ls -l /proc/self/fd
total 0
lrwx------ 1 henri henri 64 Dec 5 07:38 0 -> /dev/pts/0
lrwx------ 1 henri henri 64 Dec 5 07:38 1 -> /dev/pts/0
lrwx------ 1 henri henri 64 Dec 5 07:38 10 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:38 11 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:38 2 -> /dev/pts/0
lr-x------ 1 henri henri 64 Dec 5 07:38 3 -> /proc/2605/fd

Same w/ dash:

@@ dash
$ printf "%s\n" /dev/fd/*
/dev/fd/0
/dev/fd/1
/dev/fd/10
/dev/fd/11
/dev/fd/12
/dev/fd/17
/dev/fd/18
/dev/fd/2
/dev/fd/3
/dev/fd/55
$ ls -ld /dev/fd/*
ls: cannot access '/dev/fd/12': No such file or directory
ls: cannot access '/dev/fd/3': No such file or directory
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/0 -> /dev/pts/3
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/1 -> /dev/pts/3
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/10 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/11 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/17 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/18 -> /dev/dri/card0
lrwx------ 1 henri henri 64 Dec 5 07:25 /dev/fd/2 -> /dev/pts/3
lr-x------ 1 henri henri 64 Dec 5 07:25 /dev/fd/55 -> /home/b/henri/.local/share/baloo/index

=====


--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple

Loading...