Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using process substitution with gawk getline.

47 views
Skip to first unread message

Hongyi Zhao

unread,
Oct 8, 2016, 9:28:39 PM10/8/16
to
Hi all,

See the following:

$ awk 'BEGIN{while (( "echo 1" | getline) > 0) print $0}'
1
werner@debian-01:~$ awk 'BEGIN{while (( getline < "<( echo 1 )" ) > 0)
print $0}'
werner@debian-01:~$

Why the 2nd method failed?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Kaz Kylheku

unread,
Oct 9, 2016, 12:27:06 AM10/9/16
to
On 2016-10-09, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi all,
>
> See the following:
>
> $ awk 'BEGIN{while (( "echo 1" | getline) > 0) print $0}'
> 1
> werner@debian-01:~$ awk 'BEGIN{while (( getline < "<( echo 1 )" ) > 0)
> print $0}'
> werner@debian-01:~$

The Bash process substitution syntax works like a macro;
the syntax <(whatever ...) occurs in a shell command line and
is replaced by something else.

> Why the 2nd method failed?

Probably because the command substitution syntax <(command ...)
is not a command!

It has to be the *argument* to a command. The shell transforms
it to a special file name which a process opens as if it were
a file.

Look:

$ echo <(echo 1)
/dev/fd/63

On this platform I have here, Bash transforms command subsitutitions
into /dev/fd/.. paths.

These will only work in the intended process.

For instance, this works for me:

$ vi <(echo 1)

The Vim editor comes up, showing a buffer containing the line 1.
The status line reads:

"/dev/fd/63" [fifo/socket] 1L, 2C

This is because vi is the intended child process for which
the shell arranged /dev/fd/63.

But this doesn't work;

$ vi $(echo <(echo 1))

I get:

"/dev/fd/63" [New File]

Here, the echo process received the working /dev/fd/63 FIFO. It echoed
its name and terminated. This output was captured by $(...) syntax
and passed to vim. But /dev/fd/63 didn't exist in that process; it
only existed in the process space of the echo.

So ... even if you fix your awk getline usage so that your awk script
obtains the name which comes from the expansion of <(echo 1), it will
not be able to use that name, because it's not the child of the
command line which produced that substitution.
0 new messages