>> because this redirects the output of the bad-program, but it's the
>> shell printing the error not the program.
Dmitry's reply is more constructive but those strace arguments, all the pids
and pipe inodes, ow my head, so perhaps it's worth a simple demonstration that,
despite the undisputed truth of what Paul wrote above, the redirection still
catches the error message:
mad@shuttle:~/tmp/make-shell-2022-02-06$ bad-program 2>&1 | wc
1 5 37
mad@shuttle:~/tmp/make-shell-2022-02-06$
________________________________
From: Bug-make <[email protected]> on behalf of
Dmitry V. Levin <[email protected]>
Sent: Sunday, February 6, 2022 09:18
To: Paul Smith <[email protected]>
Cc: [email protected] <[email protected]>
Subject: Re: Bug in $(shell ...) I can't understand
***** EXTERNAL EMAIL *****
On Sun, Feb 06, 2022 at 11:23:03AM -0500, Paul Smith wrote:
> OK, someone posted a question to SO and that led me to an hour or more
> of banging my head against a wall trying to understand what's
> happening... and I can't.
>
> The problem is that the user would like to invoke $(shell ...) and
> capture errors, even errors that the program being run doesn't exist.
> The shell function only captures stdout, not stderr. This simple idea
> won't work of course:
>
> out := $(shell bad-program 2>&1)
> $(info out = $(out))
>
> $ make
> /bin/sh: bad-program: not found
> out =
>
> because this redirects the output of the bad-program, but it's the
> shell printing the error not the program.
What's actually happening is this:
strace-buildroot/usr/bin/strace -f -y -eexecve,/pipe,/dup,read,write -o'|cat
>&3' --signal=none make 3>&2 &>/dev/null
4175640 execve("/usr/bin/make", ["make"], 0x7ffdad18ea88 /* 17 vars */) = 0
4175640 read(4</lib64/libdl.so.2>,
"\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@\20\0\0\0\0\0\0"..., 832) = 832
4175640 read(4</lib64/libc.so.6>,
"\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\343\2\0\0\0\0\0"..., 832) =
832
4175640 read(4</tmp/Makefile>, "out := $(shell bad-program 2>&1)"..., 4096) = 54
4175640 pipe2([5<pipe:[1062668627]>, 6<pipe:[1062668627]>], 0) = 0
4175642 dup2(6<pipe:[1062668627]>, 1</dev/null>) = 1<pipe:[1062668627]>
4175642 execve("/bin/sh", ["/bin/sh", "-c", "bad-program 2>&1"], 0x7ffd69c8a5a8
/* 17 vars */) = 0
4175640 read(5<pipe:[1062668627]>, <unfinished ...>
4175642 read(4</lib64/libdl.so.2>,
"\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@\20\0\0\0\0\0\0"..., 832) = 832
4175642 read(4</lib64/libc.so.6>,
"\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\343\2\0\0\0\0\0"..., 832) =
832
4175642 read(4</etc/nsswitch.conf>, "#\n# Please refer to nsswitch.con"...,
4096) = 1808
4175642 read(4</etc/nsswitch.conf>, "", 4096) = 0
4175642 read(4</etc/passwd>, "root:x:0:0:System Administrator:"..., 4096) = 1326
4175643 dup2(1<pipe:[1062668627]>, 2</dev/null>) = 2<pipe:[1062668627]>
4175643 write(2<pipe:[1062668627]>, "/bin/sh: bad-program: command no"..., 40)
= 40
4175640 <... read resumed>"/bin/sh: bad-program: command no"..., 200) = 40
4175640 read(5<pipe:[1062668627]>, <unfinished ...>
4175643 +++ exited with 127 +++
4175640 <... read resumed>"", 160) = 0
4175642 +++ exited with 127 +++
4175640 write(2</dev/null>, "/bin/sh: bad-program: command no"..., 40) = 40
4175640 write(1</dev/null>, "out = ", 6) = 6
4175640 write(1</dev/null>, "\n", 1) = 1
4175640 read(4</tmp/Makefile>, "", 4096) = 0
4175640 write(2</dev/null>, "make: *** No targets. Stop.\n", 29) = 29
4175640 +++ exited with 2 +++
Try the following instead:
out := $(shell bad-program 2>&1 ||:)
$(info out = $(out))
$ make
out = /bin/sh: bad-program: command not found
The only difference is the exit status.
--
ldv