Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The solution of this problem is trivial and is left as an exercise for the reader.


devel / comp.unix.shell / How to redirect to non-standard file descriptors?

SubjectAuthor
* How to redirect to non-standard file descriptors?Robert Latest
+- Re: How to redirect to non-standard file descriptors?Lew Pitcher
+* Re: How to redirect to non-standard file descriptors?Ben Bacarisse
|`* Re: How to redirect to non-standard file descriptors?Lew Pitcher
| `* Re: How to redirect to non-standard file descriptors?Robert Latest
|  +- Re: How to redirect to non-standard file descriptors?David W. Hodgins
|  `- Re: How to redirect to non-standard file descriptors?Ben Bacarisse
+* Re: How to redirect to non-standard file descriptors?Oğuz
|+- Re: How to redirect to non-standard file descriptors?Ben Bacarisse
|`- Re: How to redirect to non-standard file descriptors?Kenny McCormack
+* Re: How to redirect to non-standard file descriptors?William Ahern
|+* Re: How to redirect to non-standard file descriptors?Robert Latest
||+- Re: How to redirect to non-standard file descriptors?William Ahern
||`* Re: How to redirect to non-standard file descriptors?Dan Espen
|| `- Re: How to redirect to non-standard file descriptors?Kenny McCormack
|`* Re: How to redirect to non-standard file descriptors?Keith Thompson
| `* Re: How to redirect to non-standard file descriptors?Geoff Clare
|  +- Re: How to redirect to non-standard file descriptors?Keith Thompson
|  `* Re: How to redirect to non-standard file descriptors?Helmut Waitzmann
|   +* Re: How to redirect to non-standard file descriptors?Ben Bacarisse
|   |`* Re: How to redirect to non-standard file descriptors?Branimir Maksimovic
|   | `- Re: How to redirect to non-standard file descriptors?Janis Papanagnou
|   `* Re: How to redirect to non-standard file descriptors?Oğuz
|    `* Re: How to redirect to non-standard file descriptors?Helmut Waitzmann
|     `* Re: How to redirect to non-standard file descriptors?Helmut Waitzmann
|      `- Re: How to redirect to non-standard file descriptors?Oğuz
`- Redirecting file descriptors (was: How to redirect to non-standard file descriptHelmut Waitzmann

Pages:12
How to redirect to non-standard file descriptors?

<irtteiFpfsqU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4443&group=comp.unix.shell#4443

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblat...@yahoo.com (Robert Latest)
Newsgroups: comp.unix.shell
Subject: How to redirect to non-standard file descriptors?
Date: 3 Oct 2021 14:36:02 GMT
Lines: 35
Message-ID: <irtteiFpfsqU1@mid.individual.net>
X-Trace: individual.net tb5ohXBKi+uFgYzcZKp5rgUQ15NZ/5kVcWPY++Jwjp1wKTmTsW
Cancel-Lock: sha1:UjiaPyQP8q6w91FHnmZfJ57fqGI=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Sun, 3 Oct 2021 14:36 UTC

Hello,
I'm trying to write a bash script that expects user input for every file in a
directory. Here's what I came up with:

#!/bin/sh

touch 1 2 3 4 5 # for testing

ls | while read fn; do
echo File $fn
read -p 'Do what:' what
echo What $what
done

It's kind of obvious why this doesn't work: Both 'read's read from the same
stdin, so instead of waiting for user input for file 1, it just reads '2' into
'what' and so on. Obviously the output of 'ls' should go into a different file
descriptor than stdout. So I tried this:

ls 1>&4 | while read -u 4 fn; do

but it gives the error "bad file descriptor".

I must say I understand very little of the redirection chapter in "man bash".
For instance, I never understood why, when I want to capture command's stderr,
I need to do

command > output.txt 2>&1

rather than

command 2>&1 > output.txt

It doesn't seem logical that a redirection specifier after the target file
would influence what goes into that file.

Re: How to redirect to non-standard file descriptors?

<sjcfft$lv8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4444&group=comp.unix.shell#4444

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Sun, 3 Oct 2021 14:42:05 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <sjcfft$lv8$1@dont-email.me>
References: <irtteiFpfsqU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 3 Oct 2021 14:42:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="14869ac6c0ea226958b8bf2eed2a6074";
logging-data="22504"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/EFR6Jd7H+6WHTQCtuHsMobmQXBeW1Z3g="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:K7JFJj1iLArlKiHSRucd7UIjOUY=
 by: Lew Pitcher - Sun, 3 Oct 2021 14:42 UTC

On Sun, 03 Oct 2021 14:36:02 +0000, Robert Latest wrote:

> Hello,
> I'm trying to write a bash script that expects user input for every file in a
> directory. Here's what I came up with:
>
> #!/bin/sh
>
> touch 1 2 3 4 5 # for testing
>
> ls | while read fn; do
> echo File $fn
> read -p 'Do what:' what
> echo What $what
> done
>
> It's kind of obvious why this doesn't work: Both 'read's read from the same
> stdin, so instead of waiting for user input for file 1, it just reads '2' into
> 'what' and so on. Obviously the output of 'ls' should go into a different file
> descriptor than stdout.

Nope.

Your script fails because both reads read from stdin, and stdin has been
redirected to the stdout of the ls(1) command.

What you /want/ is to have the first read
read fn
read from stdin (still connected to the stdout of ls(1) )

and the second read
read -p 'Do what:' what
to read from your terminal

You do this by redirecting the stdin of the /second/ read so that
it reads from /dev/tty, as in

ls | while read fn; do
echo File $fn
read -p 'Do what:' what </dev/tty
echo What $what
done

[snip]

--
Lew Pitcher
"In Skills, We Trust"

Re: How to redirect to non-standard file descriptors?

<87v92eds5r.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4445&group=comp.unix.shell#4445

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Sun, 03 Oct 2021 16:50:08 +0100
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <87v92eds5r.fsf@bsb.me.uk>
References: <irtteiFpfsqU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="0f9923d1833c2f42daa3b77525dc9b37";
logging-data="30487"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/A/tZr/yULIKxCX1Q1BWiwEJUbieAJPpk="
Cancel-Lock: sha1:SoX+sMmcEJgZIryBdEqw5/NoADE=
sha1:8CTl1s7G2eNGIwQTaqRI/NjJPlM=
X-BSB-Auth: 1.e35fb0717718c196d83f.20211003165008BST.87v92eds5r.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 3 Oct 2021 15:50 UTC

Robert Latest <boblatest@yahoo.com> writes:

> I'm trying to write a bash script that expects user input for every file in a
> directory. Here's what I came up with:
>
> #!/bin/sh
>
> touch 1 2 3 4 5 # for testing
>
> ls | while read fn; do
> echo File $fn
> read -p 'Do what:' what
> echo What $what
> done
>
> It's kind of obvious why this doesn't work: Both 'read's read from the same
> stdin, so instead of waiting for user input for file 1, it just reads '2' into
> 'what' and so on. Obviously the output of 'ls' should go into a different file
> descriptor than stdout.

You've had an answer, but I don't think it's ideal. Unix works best by
using stdin and stout where possible. One day you'll want the answers
to come from a file and not the user's tty. (And if you don't someone
else will!)

You should simply side-step the two reads by looping over the files
directly:

for fn in *; do
echo File "$fn"
read -p 'Do what:' what
echo What "$what"
done

Note that I've also quoted all uses of the variables.

> ls 1>&4 | while read -u 4 fn; do

You may be able to do it with read's -u n argument, but I've never found
the need to delve into that sort of thing. Simpler is better wherever
it's possible.

--
Ben.

Re: How to redirect to non-standard file descriptors?

<sjcka4$lv8$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4446&group=comp.unix.shell#4446

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Sun, 3 Oct 2021 16:04:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <sjcka4$lv8$2@dont-email.me>
References: <irtteiFpfsqU1@mid.individual.net> <87v92eds5r.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 3 Oct 2021 16:04:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="14869ac6c0ea226958b8bf2eed2a6074";
logging-data="22504"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IdrqJp9BBZfKkBxQkDjZ+2FYgHd2D1yA="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:AUC6ijinTJPbXkE7n+2FfXSgULI=
 by: Lew Pitcher - Sun, 3 Oct 2021 16:04 UTC

On Sun, 03 Oct 2021 16:50:08 +0100, Ben Bacarisse wrote:

> Robert Latest <boblatest@yahoo.com> writes:
>
>> I'm trying to write a bash script that expects user input for every file in a
>> directory. Here's what I came up with:
>>
>> #!/bin/sh
>>
>> touch 1 2 3 4 5 # for testing
>>
>> ls | while read fn; do
>> echo File $fn
>> read -p 'Do what:' what
>> echo What $what
>> done
>>
>> It's kind of obvious why this doesn't work: Both 'read's read from the same
>> stdin, so instead of waiting for user input for file 1, it just reads '2' into
>> 'what' and so on. Obviously the output of 'ls' should go into a different file
>> descriptor than stdout.
>
> You've had an answer, but I don't think it's ideal. Unix works best by
> using stdin and stout where possible. One day you'll want the answers
> to come from a file and not the user's tty. (And if you don't someone
> else will!)

I bow to your expertise. Your solution covers more situations than mine
did. I learned something today :-)

[snip]
--
Lew Pitcher
"In Skills, We Trust"

Re: How to redirect to non-standard file descriptors?

<iru71eFr8gpU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4447&group=comp.unix.shell#4447

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblat...@yahoo.com (Robert Latest)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: 3 Oct 2021 17:19:42 GMT
Lines: 23
Message-ID: <iru71eFr8gpU1@mid.individual.net>
References: <irtteiFpfsqU1@mid.individual.net> <87v92eds5r.fsf@bsb.me.uk>
<sjcka4$lv8$2@dont-email.me>
X-Trace: individual.net 82NAHkMb8TwWsVeYpvdIpwbSrnAg/l5Tpz1DjbBvRaqZ0sjfyj
Cancel-Lock: sha1:ijFOingcX6Ae/uj8q2KKdqBaLEc=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Sun, 3 Oct 2021 17:19 UTC

Lew Pitcher wrote:
> On Sun, 03 Oct 2021 16:50:08 +0100, Ben Bacarisse wrote:
> I bow to your expertise. Your solution covers more situations than mine
> did. I learned something today :-)

Ben, Lew,

I bow to both of your's expertise. I usually do the for loop myself but in
this case the input doesn't come from a simple 'ls' but from a more convoluted
thing which of course could be backquoted and used in 'for', but the filenames
may have spaces in it and so on... anyway, I used the /dev/tty redirect.

More weird stuff seems to be possible with 'exec'; here's some stuff I found in
/sbin:

/sbin$ grep '>&[3-9]' *
dhclient-script: exec 0>&9 9>&-
discover-pkginstall: echo $RET | sed 's/,//g' 1>&8
mkinitramfs: exec 4>&1 >&3 3>&-
mkinitramfs: find . 4>&-; echo "ec1=$?;" >&4
mkinitramfs: echo "ec2=$?;" >&4

I don't understand it and don't need it at the moment.

Re: How to redirect to non-standard file descriptors?

<op.1ao68ubta3w0dxdave@hodgins.homeip.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4448&group=comp.unix.shell#4448

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dwhodg...@nomail.afraid.org (David W. Hodgins)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Sun, 03 Oct 2021 13:37:32 -0400
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <op.1ao68ubta3w0dxdave@hodgins.homeip.net>
References: <irtteiFpfsqU1@mid.individual.net> <87v92eds5r.fsf@bsb.me.uk>
<sjcka4$lv8$2@dont-email.me> <iru71eFr8gpU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="041caecdaec88f18ebba7dd8ca4fced7";
logging-data="15737"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191Hw5q5L2GHH+hof7QL7m2b/8dWpCW7HM="
User-Agent: Opera Mail/12.16 (Linux)
Cancel-Lock: sha1:WDq5JQfFZxTyRR5c9KsO+OWRfnk=
 by: David W. Hodgins - Sun, 3 Oct 2021 17:37 UTC

On Sun, 03 Oct 2021 13:19:42 -0400, Robert Latest <boblatest@yahoo.com> wrote:
> I bow to both of your's expertise. I usually do the for loop myself but in
> this case the input doesn't come from a simple 'ls' but from a more convoluted
> thing which of course could be backquoted and used in 'for', but the filenames
> may have spaces in it and so on... anyway, I used the /dev/tty redirect.

Then use an array, and for the spaces etc, be sure to always properly quote the
variables. For example ...
IFS=$'\n'
SFDiskOutput=($($sudocmd $sfdiskcmd \-l \-uS /dev/sd? 2>&1)) # list drives and block sizes
<snip test for error etc.>
for SFDiskLine in "${SFDiskOutput[@]}" ; do

Regards, Dave Hodgins

--
Change dwhodgins@nomail.afraid.org to davidwhodgins@teksavvy.com for
email replies.

Re: How to redirect to non-standard file descriptors?

<87pmslet3a.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4449&group=comp.unix.shell#4449

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Sun, 03 Oct 2021 21:44:41 +0100
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <87pmslet3a.fsf@bsb.me.uk>
References: <irtteiFpfsqU1@mid.individual.net> <87v92eds5r.fsf@bsb.me.uk>
<sjcka4$lv8$2@dont-email.me> <iru71eFr8gpU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="0f9923d1833c2f42daa3b77525dc9b37";
logging-data="1687"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pvp0IVXt+XwXDYQCiPhZvYdB3XK5b02s="
Cancel-Lock: sha1:56DkitirmSJRqMT9Htd5T76+ePc=
sha1:5HK7tW3qepakBPm79i1ahGI3dDc=
X-BSB-Auth: 1.cdf71a571e8dac28ae77.20211003214441BST.87pmslet3a.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 3 Oct 2021 20:44 UTC

Robert Latest <boblatest@yahoo.com> writes:

> Lew Pitcher wrote:
>> On Sun, 03 Oct 2021 16:50:08 +0100, Ben Bacarisse wrote:
>> I bow to your expertise. Your solution covers more situations than mine
>> did. I learned something today :-)
>
> Ben, Lew,
>
> I bow to both of your's expertise. I usually do the for loop myself
> but in this case the input doesn't come from a simple 'ls' but from a
> more convoluted thing which of course could be backquoted and used in
> 'for', but the filenames may have spaces in it and so on... anyway, I
> used the /dev/tty redirect.

Presumably the "and so on" does not include newlines because they will
cause the 'read' method to fail. If that's not a problem you can use

IFS=$'\n'
for fn in $(complex command); do ... done

If you want to manage every single file (ones with newlines included)
you need something that can use a null character as the separator. For
example

find . -maxdepth 1 -print0

is very much like ls but separates the names with null bytes. If you
can arrange that sort of input, you can get the names into an array in a
modern version of bash using

readarray -d '' files < <(find . -maxdepth 1 -print0)

The -d '' makes readarray use null as an input separator (something you
can't do with IFS) and you can then loop over the array

for fn in "${files[@]}"; do ... done

--
Ben.

Re: How to redirect to non-standard file descriptors?

<25460292-f17f-4c82-b06d-01d30cf2cec0n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4450&group=comp.unix.shell#4450

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:209:: with SMTP id b9mr10663809qtx.28.1633308659208;
Sun, 03 Oct 2021 17:50:59 -0700 (PDT)
X-Received: by 2002:ac8:5707:: with SMTP id 7mr10654295qtw.79.1633308659048;
Sun, 03 Oct 2021 17:50:59 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 3 Oct 2021 17:50:58 -0700 (PDT)
In-Reply-To: <irtteiFpfsqU1@mid.individual.net>
Injection-Info: google-groups.googlegroups.com; posting-host=88.246.88.104; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 88.246.88.104
References: <irtteiFpfsqU1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <25460292-f17f-4c82-b06d-01d30cf2cec0n@googlegroups.com>
Subject: Re: How to redirect to non-standard file descriptors?
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Mon, 04 Oct 2021 00:50:59 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 43
 by: Oğuz - Mon, 4 Oct 2021 00:50 UTC

On Sunday, October 3, 2021 at 5:36:08 PM UTC+3, Robert Latest wrote:
> Hello,
> I'm trying to write a bash script that expects user input for every file in a
> directory. Here's what I came up with:
>
> #!/bin/sh
>
> touch 1 2 3 4 5 # for testing
>
> ls | while read fn; do
> echo File $fn
> read -p 'Do what:' what
> echo What $what
> done
>
> It's kind of obvious why this doesn't work: Both 'read's read from the same
> stdin, so instead of waiting for user input for file 1, it just reads '2' into
> 'what' and so on. Obviously the output of 'ls' should go into a different file
> descriptor than stdout.

No, but the inner read should read from the shell's input:
{ ls | while read fn; do
echo File $fn
read -p 'Do what:' -u 3 what
echo What $what
done
} 3<&0

>
> but it gives the error "bad file descriptor".
>
> I must say I understand very little of the redirection chapter in "man bash".
> For instance, I never understood why, when I want to capture command's stderr,
> I need to do
>
> command > output.txt 2>&1
>
> rather than
>
> command 2>&1 > output.txt
>
> It doesn't seem logical that a redirection specifier after the target file
> would influence what goes into that file.

Re: How to redirect to non-standard file descriptors?

<87bl45mw57.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4451&group=comp.unix.shell#4451

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Mon, 04 Oct 2021 02:11:32 +0100
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <87bl45mw57.fsf@bsb.me.uk>
References: <irtteiFpfsqU1@mid.individual.net>
<25460292-f17f-4c82-b06d-01d30cf2cec0n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="485f6af566060d769957028d86f88dab";
logging-data="32559"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5Buvmi/EKOzdXHQCudzjZoCCp2nUCwL4="
Cancel-Lock: sha1:0U8xXFGEUxuO8O4Op2iugunu8Uo=
sha1:5+zbiSazM2Mpm+R3skZEk9F4iIA=
X-BSB-Auth: 1.f12f0e74b8818454f216.20211004021132BST.87bl45mw57.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 4 Oct 2021 01:11 UTC

Oğuz <oguzismailuysal@gmail.com> writes:

> On Sunday, October 3, 2021 at 5:36:08 PM UTC+3, Robert Latest wrote:
>> Hello,
>> I'm trying to write a bash script that expects user input for every file in a
>> directory. Here's what I came up with:
>>
>> #!/bin/sh
>>
>> touch 1 2 3 4 5 # for testing
>>
>> ls | while read fn; do
>> echo File $fn
>> read -p 'Do what:' what
>> echo What $what
>> done
>>
>> It's kind of obvious why this doesn't work: Both 'read's read from the same
>> stdin, so instead of waiting for user input for file 1, it just reads '2' into
>> 'what' and so on. Obviously the output of 'ls' should go into a different file
>> descriptor than stdout.
>
> No, but the inner read should read from the shell's input:
> {
> ls | while read fn; do
> echo File $fn
> read -p 'Do what:' -u 3 what
> echo What $what
> done
> } 3<&0

This is a nice solution as it does not prevent redirection of the
command's input. (The usual caveats about newlines in file names, and
the 'ls' being, in practice, a more complex command.)

--
Ben.

Re: How to redirect to non-standard file descriptors?

<sjdn8s$2t8n5$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4452&group=comp.unix.shell#4452

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Mon, 4 Oct 2021 02:01:00 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sjdn8s$2t8n5$1@news.xmission.com>
References: <irtteiFpfsqU1@mid.individual.net> <25460292-f17f-4c82-b06d-01d30cf2cec0n@googlegroups.com>
Injection-Date: Mon, 4 Oct 2021 02:01:00 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3056357"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 4 Oct 2021 02:01 UTC

In article <25460292-f17f-4c82-b06d-01d30cf2cec0n@googlegroups.com>,
O uz <oguzismailuysal@gmail.com> wrote:
....
>No, but the inner read should read from the shell's input:
>{
> ls | while read fn; do
> echo File $fn
> read -p 'Do what:' -u 3 what
> echo What $what
> done
>} 3<&0

Or, somewhat more simply:

#!/bin/bash
exec 3<&0
seq 1 10 | while read fn; do
echo File $fn
read -p 'Do what:' -u3 what
echo What $what
done

Or even:

#!/bin/bash
exec 3<&0
while read fn; do
echo File $fn
read -p 'Do what:' -u3 what
echo What $what
done < <(seq 1 10)

Which often works better than the first method...

But, as noted many times in this thread, if you want to loop on filenames,
it is better to do:

for i in FOO*.*;do ...

--
To most Christians, the Bible is like a software license. Nobody
actually reads it. They just scroll to the bottom and click "I agree."

- author unknown -

Re: How to redirect to non-standard file descriptors?

<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4453&group=comp.unix.shell#4453

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Mon, 04 Oct 2021 23:30:02 -0500
Message-ID: <sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
From: will...@25thandClement.com (William Ahern)
Subject: Re: How to redirect to non-standard file descriptors?
Newsgroups: comp.unix.shell
References: <irtteiFpfsqU1@mid.individual.net>
User-Agent: tin/2.4.4-20191224 ("Millburn") (OpenBSD/6.9 (amd64))
Date: Mon, 4 Oct 2021 21:28:12 -0700
Lines: 78
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-ic7TkbAz5QFrin9NmKxsXOydpJkKvmAqgNkzxToMpsB3Di5Jg44OYimu8Uik20EHix+2/e5W+C1ZqTP!wVEsEPakK1kw1SDtqSxrlvQnbIDzsIy2vw5fVFGouQtOFpgvttv0DFxVgcZnxx7c+yA=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3463
 by: William Ahern - Tue, 5 Oct 2021 04:28 UTC

Robert Latest <boblatest@yahoo.com> wrote:
<snip>
> I must say I understand very little of the redirection chapter in "man bash".
> For instance, I never understood why, when I want to capture command's stderr,
> I need to do
>
> command > output.txt 2>&1
>
> rather than
>
> command 2>&1 > output.txt
>
> It doesn't seem logical that a redirection specifier after the target file
> would influence what goes into that file.

It makes more sense when you understand how redirection is implemented in C:

2>&1

evaulates as

dup2(1, 2);

and

>output.txt

is short-hand for

1>output.txt

which evaluates as

int fd = open("output.txt", O_CREAT|O_RDONLY);
dup2(fd, 1);

Now consider that redirection operators as well as the pipe operator (|) are
evaluated left to right, and that after performing dup2(1, 2) there's no way
to recover the previous file which descriptor 2 referenced. If you want to
reference that previous file you either need to rearrange the order of
operations or explicitly copy (dup) the reference to another descriptor
number.

The Unix shell makes more sense when you realize that most seemingly
abstract features directly map to a small set of otherwise simple syscalls
such as dup, dup2, fork, waitpid, etc. At least in the context of Unix
systems, the shell excels at program execution and I/O redirection precisely
because it's effectively a literal, in-order evaluation of these syscalls.

command > output.txt 2>&1

evaluates much like

int pid = fork();
if (pid == 0) { /* child */
int fd = open("output.txt");
dup2(fd, 1);
dup2(1, 2); /* fd:1 is now output.txt, not old stdout */
execv("command", ...);
exit(127);
} else { /* parent */
int status;
while (pid != wait(&status)) {
...
}
}

Most of the complexity of shell implementations is in string manipulation
and other bookkeeping that's relatively verbose in C, as well as interactive
features like managing the prompt. Early shell implementations, before many
of those other convenience features were added, were thus exceedingly simple
interpreters. Here's the code to the original Bourne shell code that parsed
and executed a command-line:

https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/xec.c

Which at cursory inspection seems to evaluate a command as I described,
except implemented as a loop and switch over the scanner.

Re: How to redirect to non-standard file descriptors?

<is3hp4Fsa3fU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4454&group=comp.unix.shell#4454

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblat...@yahoo.com (Robert Latest)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: 5 Oct 2021 17:53:40 GMT
Lines: 20
Message-ID: <is3hp4Fsa3fU1@mid.individual.net>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
X-Trace: individual.net HwK7ZrF3gOvf7rR+cmgv6Am4cns9PDKqmv1ZvMR68iWyejF67o
Cancel-Lock: sha1:r8BGbWqcm0w1dX8oS3KIHFJK7Kc=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Tue, 5 Oct 2021 17:53 UTC

William Ahern wrote:
>
> It makes more sense when you understand how redirection is implemented in C:
>
> 2>&1
>
> evaulates as
>
> dup2(1, 2);

I must admit I've never gotten into the low-level I/O stuff always ised the
more high-level stdio implementation.

>
> https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/xec.c

Now that's some weird-looking code. There must be some interesting stuff in
sym.h and defs.h.

Thanks for the good explanation!

Re: How to redirect to non-standard file descriptors?

<87mtnn8ewy.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4455&group=comp.unix.shell#4455

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Tue, 05 Oct 2021 12:11:57 -0700
Organization: None to speak of
Lines: 55
Message-ID: <87mtnn8ewy.fsf@nosuchdomain.example.com>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="96fd2c4aa8876f2cf843b5bf82d8783f";
logging-data="22346"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vIj+RvoskqP0bKOdF+ZGj"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:MSw8znjlAKVrZDfdaMdK8lq1TlU=
sha1:FouZiRZvN9ufP8UTq0McxoYMQuA=
 by: Keith Thompson - Tue, 5 Oct 2021 19:11 UTC

William Ahern <william@25thandClement.com> writes:
> Robert Latest <boblatest@yahoo.com> wrote:
> <snip>
>> I must say I understand very little of the redirection chapter in "man bash".
>> For instance, I never understood why, when I want to capture command's stderr,
>> I need to do
>>
>> command > output.txt 2>&1
>>
>> rather than
>>
>> command 2>&1 > output.txt
>>
>> It doesn't seem logical that a redirection specifier after the target file
>> would influence what goes into that file.
>
> It makes more sense when you understand how redirection is implemented in C:

[rest of thorough explanation snipped]

Personally, I find it easier to understand the syntax by thinking of it
as a left-to-right evaluation of operations, without reference to the
dup2() calls.

command # executes command with stdout and stderr inherited from the shell
> output.txt # redirects stdout to file "output.txt", leaving stderr alone
2>&1 # redirects stderr to wherever stdout is currently going

Result: both stdout and stderr are redirected to the file "output.txt".
(Of course the command doesn't start to execute until all the
redirections have been done.)

Compare the other suggested command:

command # executes command with stdout and stderr inherited from the shell
2>&1 # redirects stderr to wherever stdout is currently going
> output.txt # redirects stdout to "output.txt", leaving stderr alone

Result: stdout goes to "output.txt", but stderr is unchanged.

I think the key point of confusion (I used to get hung up on this
myself) is that "2>&1" doesn't say "make stdout and stderr go to the
same place, and keep them joined over any future redirections". It's a
one-time change to stderr.

If it helps, think of stdin (0), stdout (1), and stderr (2) as
independent variables, and the redirection operators as assignment
statements, evaluated left to right. The "assignment "2>&1" changes the
value of fd 2, but does not affect fd 1; it only obtains fd 1's current
value.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Redirecting file descriptors (was: How to redirect to non-standard file descriptors?)

<835yubi6vk.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4456&group=comp.unix.shell#4456

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!news.mixmin.net!aioe.org!UvG48Vl/yBcPk1+31nlajw.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Redirecting file descriptors (was: How to redirect to non-standard file descriptors?)
Date: Tue, 05 Oct 2021 21:55:27 +0200
Organization: Aioe.org NNTP Server
Message-ID: <835yubi6vk.fsf@helmutwaitzmann.news.arcor.de>
References: <irtteiFpfsqU1@mid.individual.net>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="55094"; posting-host="UvG48Vl/yBcPk1+31nlajw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
Cancel-Lock: sha1:HfW0lRP87AGuti5GVN7YTNYaW6w=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Tue, 5 Oct 2021 19:55 UTC

Robert Latest <boblatest@yahoo.com>:

>I must say I understand very little of the redirection chapter in
>"man bash".

That's because the bash manual page purpose is not to teach the unix
file descriptor redirection mechanism.  The bash manual page assumes
that the reader already is familiar with the unix file descriptors
and knows what “redirection” of those file descriptors is.

So, here is a very short introduction to unix file descriptors:

For accessing files, the kernel maintains two data structures: a
system-wide table (or array) of opened files, and a per-process
(i. e. each process has one of its own) table (or array) of file
descriptors.  When a process asks the kernel to open a file, for
example by invoking the “open()” system service, the kernel picks an
unused entry of the process' table of file descriptors.  This entry
will be identified by its position (or index) in that file
descriptor table: a small number (0, 1, 2, …), a. k. a. the file
descriptor number.

Then the kernel picks an unused entry of the system-wide table of
open files and records a reference to (for example: the position in
the system‐wide open‐files table) that entry in the allocated entry
of the process' table of file descriptors.

In the allocated entry of the system-wide table of open files, the
kernel records, which file is to be accessed, the access mode (that
is, whether the file is opened for reading or for writing, if
writing will overwrite the file or append to it, etc.), the current
access position in the opened file, and, how many file descriptor
table entries are associated with this entry of the system-wide
table of open files (in this case: 1).

Finally, the kernel returns the index of the allocated entry of the
process' file descriptor table to the process.  In the bash manual
page that index is known as file descriptor number.

For example, the return value of the system call “open()” will
return such a number.  See the manual page “open(2)”.

Note:  The process can't (directly) tell the kernel, which entry
of the file descriptors table to use, when opening a file.

But there is a system service, which allows a process to tell the
kernel, which entry of the file descriptors table to use: “dup2()”. 
See the manual page “dup2(2)”.  The “dup()” and “dup2()” system
services essentially copy one entry of the process' file descriptor
table to another.

So, if you tell the shell to start a command by

command > output.txt

the shell will first open the file “output.txt” by means of the
system service “open”, which will (for example) return the file
descriptor number 42.  Then the shell will tell the kernel by means
of the system service “dup2” to copy the contents of the entry #42
of the process' file descriptor table to the entry #1 in the same
table.  Finally the shell will tell the kernel by means of the
system service “close” to release (i. e. empty) the entry #42.

The result of those three system service calls is, that the file
descriptor #1 will refer to the opened file “output.txt” (rather
than to the terminal):  When the command writes data to its file
descriptor #1 (a. k. a. standard output), the data will arrive in
the file “output.txt”.  That's why this sequence of system service
calls is often called redirection.  But note:  There is nothing like
redirection or forwarding involved in this mechanism.  It's just the
effect of copying one file descriptor table entry to another.

Now, if the command is

> command > output.txt 2>&1
>

the shell first will do the same and then tell the kernel by means
of the system service “dup2” to copy the contents of the entry #1 to
the entry #2.  Now two entries in the process' file descriptor
table – #1 and #2 – refer to the opened file “output.txt”.

On the other hand, look, what

> command 2>&1 > output.txt
>

would do:  It would copy the entry #1 of the process' file
descriptor table to the entry #2 and then open the file
“output.txt”, thus getting the (hypothetical) entry #42, then copy
that entry #42 to the entry #1.  Of course the entry #2 won't be a
copy of the entry #42 then, i. e. won't refer to the file
“output.txt”.

>It doesn't seem logical that a redirection specifier after the
>target file would influence what goes into that file.

You are fooled by the misnomer file descriptor redirection.  Keep in
mind, that

2>&1

is essentially not much more than copying entry #1 to entry #2. 
There is no forwarding involved like “if you want to write to the
file referred by the process' file descriptor table entry #2, look
up the file descriptor table entry #1 and use that instead”.

See also <https://en.wikipedia.org/wiki/File_descriptor#top>.

Re: How to redirect to non-standard file descriptors?

<npvu2i-58m1.ln1@wilbur.25thandClement.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4457&group=comp.unix.shell#4457

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!feeder1.feed.usenet.farm!feed.usenet.farm!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 05 Oct 2021 17:30:02 -0500
Message-ID: <npvu2i-58m1.ln1@wilbur.25thandClement.com>
From: will...@25thandClement.com (William Ahern)
Subject: Re: How to redirect to non-standard file descriptors?
Newsgroups: comp.unix.shell
References: <irtteiFpfsqU1@mid.individual.net> <sr0t2i-cjm2.ln1@wilbur.25thandClement.com> <is3hp4Fsa3fU1@mid.individual.net>
User-Agent: tin/2.4.4-20191224 ("Millburn") (OpenBSD/6.9 (amd64))
Date: Tue, 5 Oct 2021 15:22:15 -0700
Lines: 22
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-zIvUAMU1DP6aqjR5XmcA/BzDlsqnlN1YmcEGeSbuGX2iTMRiOeokYmMMsq/mFhWxrYM8ar9SvTsNPb+!PBFka42Da22rYDTeLDwOn4sO2Y5w8WXNHycaU9+8FBOUsM72JT8VZrNofzIb1fc8nrY=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1707
 by: William Ahern - Tue, 5 Oct 2021 22:22 UTC

Robert Latest <boblatest@yahoo.com> wrote:
> William Ahern wrote:
>>
>> It makes more sense when you understand how redirection is implemented in C:
>>
>> 2>&1
>>
>> evaulates as
>>
>> dup2(1, 2);
>
> I must admit I've never gotten into the low-level I/O stuff always ised the
> more high-level stdio implementation.
>
>>
>> https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/xec.c
>
> Now that's some weird-looking code. There must be some interesting stuff in
> sym.h and defs.h.

Steve Bourne fancied Algol syntax and blazed the trail for abusive C
preprocessor magic: https://research.swtch.com/shmacro

Re: How to redirect to non-standard file descriptors?

<sjit33$133$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4458&group=comp.unix.shell#4458

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dan1es...@gmail.com (Dan Espen)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Tue, 05 Oct 2021 21:10:59 -0400
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <sjit33$133$2@dont-email.me>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<is3hp4Fsa3fU1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b8d3b9ef98415b4f04a815da30218c4a";
logging-data="1123"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18P6FnKMY7PKLVdl1NGCoDw/qSl8E24Teg="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:0rilJcxqNLrHKmmq1R0I6ZlANVQ=
 by: Dan Espen - Wed, 6 Oct 2021 01:10 UTC

Robert Latest <boblatest@yahoo.com> writes:

> William Ahern wrote:
>>
>> It makes more sense when you understand how redirection is implemented in C:
>>
>> 2>&1
>>
>> evaulates as
>>
>> dup2(1, 2);
>
> I must admit I've never gotten into the low-level I/O stuff always ised the
> more high-level stdio implementation.
>
>>
>> https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/xec.c
>
> Now that's some weird-looking code. There must be some interesting stuff in
> sym.h and defs.h.

I like it. No curly braces except at the function level.
Somebody messed up SYSTIMES and SYSUMASK.

--
Dan Espen

Re: How to redirect to non-standard file descriptors?

<sjjspd$303ug$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4459&group=comp.unix.shell#4459

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Wed, 6 Oct 2021 10:11:57 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sjjspd$303ug$1@news.xmission.com>
References: <irtteiFpfsqU1@mid.individual.net> <sr0t2i-cjm2.ln1@wilbur.25thandClement.com> <is3hp4Fsa3fU1@mid.individual.net> <sjit33$133$2@dont-email.me>
Injection-Date: Wed, 6 Oct 2021 10:11:57 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3149776"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Wed, 6 Oct 2021 10:11 UTC

In article <sjit33$133$2@dont-email.me>,
Dan Espen <dan1espen@gmail.com> wrote:
....
>> https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/xec.c
>>
>> Now that's some weird-looking code. There must be some interesting stuff in
>> sym.h and defs.h.
>
>I like it. No curly braces except at the function level.
>Somebody messed up SYSTIMES and SYSUMASK.

As another poster noted, the syntax is reminiscent of Algol, but what is
most notable is that it is the same (more or less) syntax as the shell
itself. (IF/FI, etc).

The syntax of the shell itself was, they say, based on Algol.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/TedCruz

Re: How to redirect to non-standard file descriptors?

<n2i03i-vg.ln1@ID-313840.user.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4460&group=comp.unix.shell#4460

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geo...@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Wed, 6 Oct 2021 13:40:23 +0100
Lines: 20
Message-ID: <n2i03i-vg.ln1@ID-313840.user.individual.net>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net rl8+E9BpSw5z4ZtfMlcVVQd7mt38Fp8djnBMyFkdDWStHCCoHH
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:nCy3pPOE6qa0VkjgGdrvNONLCM8=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Wed, 6 Oct 2021 12:40 UTC

Keith Thompson wrote:

> command # executes command with stdout and stderr inherited from the shell
> 2>&1 # redirects stderr to wherever stdout is currently going
>> output.txt # redirects stdout to "output.txt", leaving stderr alone
>
> Result: stdout goes to "output.txt", but stderr is unchanged.

Nit-pick: if stdout and stderr were open to different files before
the command, stderr is not unchanged; it ends up going to the file
that stdout was originally going to.

A typical use of this is to filter stderr:

some_command 2>&1 >output.txt | some_filter >&2

The 2>&1 makes stderr go to the pipe.

--
Geoff Clare <netnews@gclare.org.uk>

Re: How to redirect to non-standard file descriptors?

<875yua8cqh.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4461&group=comp.unix.shell#4461

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Wed, 06 Oct 2021 07:11:18 -0700
Organization: None to speak of
Lines: 25
Message-ID: <875yua8cqh.fsf@nosuchdomain.example.com>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
<n2i03i-vg.ln1@ID-313840.user.individual.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="fa80d595787fe04fe31c20e786e6f9a8";
logging-data="15934"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196D+OyaP6+FofX4xns98/F"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:ex5gQXLNgvlyM5UtgW1J0beIg9Q=
sha1:UQ22Ll1qaXOE8/OHll4Yd1le81w=
 by: Keith Thompson - Wed, 6 Oct 2021 14:11 UTC

Geoff Clare <geoff@clare.See-My-Signature.invalid> writes:
> Keith Thompson wrote:
>
>> command # executes command with stdout and stderr inherited from the shell
>> 2>&1 # redirects stderr to wherever stdout is currently going
>>> output.txt # redirects stdout to "output.txt", leaving stderr alone
>>
>> Result: stdout goes to "output.txt", but stderr is unchanged.
>
> Nit-pick: if stdout and stderr were open to different files before
> the command, stderr is not unchanged; it ends up going to the file
> that stdout was originally going to.

Quite correct. (That's not just a nitpick; I was wrong.) Thanks.

> A typical use of this is to filter stderr:
>
> some_command 2>&1 >output.txt | some_filter >&2
>
> The 2>&1 makes stderr go to the pipe.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to redirect to non-standard file descriptors?

<83v929gucn.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4462&group=comp.unix.shell#4462

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!aioe.org!Kv1NnmzEegNy3OWSoPgydA.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Thu, 07 Oct 2021 09:35:52 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83v929gucn.fsf@helmutwaitzmann.news.arcor.de>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
<n2i03i-vg.ln1@ID-313840.user.individual.net>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="45863"; posting-host="Kv1NnmzEegNy3OWSoPgydA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
X-Notice: Filtered by postfilter v. 0.9.2
Mail-Copies-To: nobody
Cancel-Lock: sha1:MMnu47n6s3+KJbdnBGcut5ea+YY=
 by: Helmut Waitzmann - Thu, 7 Oct 2021 07:35 UTC

Geoff Clare <geoff@clare.See-My-Signature.invalid>:

>A typical use of this is to filter stderr:
>
>some_command 2>&1 >output.txt | some_filter >&2
>
>The 2>&1 makes stderr go to the pipe.
>

It's even possible to filter stdout as well as stderr, each of them
by a filter of its own, for example:

{
{ some_command 3>&- 4>&- |
sed -e 's/^/stdout: /' 1>&3 3>&- 2>&4 4>&-
} 2>&1 |
sed -e 's/^/stderr: /' 1>&2 4>&- 3>&-
} 3>&1 4>&2

Re: How to redirect to non-standard file descriptors?

<87h7dscw7k.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4463&group=comp.unix.shell#4463

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Thu, 07 Oct 2021 23:21:35 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <87h7dscw7k.fsf@bsb.me.uk>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
<n2i03i-vg.ln1@ID-313840.user.individual.net>
<83v929gucn.fsf@helmutwaitzmann.news.arcor.de>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="2b3fe689dcdd84d60d847c36de22a538";
logging-data="6961"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xN/N0xEUgAWae/OfnNWu7+SC0TNb0F38="
Cancel-Lock: sha1:gHjriqJ0RY18VyvjFSIdEDk79cs=
sha1:fcqiqm6enVgdhV4W2D6VtQs0Hu0=
X-BSB-Auth: 1.90ff444f1d6cc64cf54b.20211007232135BST.87h7dscw7k.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 7 Oct 2021 22:21 UTC

Helmut Waitzmann <nn.throttle@xoxy.net> writes:

> Geoff Clare <geoff@clare.See-My-Signature.invalid>:
>
>>A typical use of this is to filter stderr:
>>
>>some_command 2>&1 >output.txt | some_filter >&2
>>
>>The 2>&1 makes stderr go to the pipe.
>>
>
> It's even possible to filter stdout as well as stderr, each of them by a filter of its own, for example:
>
> {
> { some_command 3>&- 4>&- |
> sed -e 's/^/stdout: /' 1>&3 3>&- 2>&4 4>&-
> } 2>&1 |
> sed -e 's/^/stderr: /' 1>&2 4>&- 3>&-
> } 3>&1 4>&2

Wow. I take my hat off to you. I knew this was possible, I don't think
I'd be able to work out how. But it is an illustration of design
failure. This should not be hard.

(When I need to do this, I used a named pipe.)

--
Ben.

Re: How to redirect to non-standard file descriptors?

<2sL7J.103836$jl2.15629@fx34.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4464&group=comp.unix.shell#4464

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx34.iad.POSTED!not-for-mail
Newsgroups: comp.unix.shell
From: branimir...@icloud.com (Branimir Maksimovic)
Subject: Re: How to redirect to non-standard file descriptors?
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
<n2i03i-vg.ln1@ID-313840.user.individual.net>
<83v929gucn.fsf@helmutwaitzmann.news.arcor.de> <87h7dscw7k.fsf@bsb.me.uk>
User-Agent: slrn/1.0.3 (Darwin)
Lines: 32
Message-ID: <2sL7J.103836$jl2.15629@fx34.iad>
X-Complaints-To: abuse@usenet-news.net
NNTP-Posting-Date: Thu, 07 Oct 2021 23:32:14 UTC
Organization: usenet-news.net
Date: Thu, 07 Oct 2021 23:32:14 GMT
X-Received-Bytes: 1801
 by: Branimir Maksimovic - Thu, 7 Oct 2021 23:32 UTC

On 2021-10-07, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Helmut Waitzmann <nn.throttle@xoxy.net> writes:
>
>> Geoff Clare <geoff@clare.See-My-Signature.invalid>:
>>
>>>A typical use of this is to filter stderr:
>>>
>>>some_command 2>&1 >output.txt | some_filter >&2
>>>
>>>The 2>&1 makes stderr go to the pipe.
>>>
>>
>> It's even possible to filter stdout as well as stderr, each of them by a
>> filter of its own, for example:
>>
>> { { some_command 3>&- 4>&- | sed -e 's/^/stdout: /' 1>&3 3>&- 2>&4 4>&- }
>> 2>&1 | sed -e 's/^/stderr: /' 1>&2 4>&- 3>&- } 3>&1 4>&2
>
> Wow. I take my hat off to you. I knew this was possible, I don't think I'd
> be able to work out how. But it is an illustration of design failure. This
> should not be hard.
>
> (When I need to do this, I used a named pipe.)
>
It's easy to read, it' just not common usage :P

--

7-77-777
Evil Sinner!
with software, you repeat same experiment, expecting different results...

Re: How to redirect to non-standard file descriptors?

<sjora4$2vo$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4465&group=comp.unix.shell#4465

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Fri, 8 Oct 2021 09:17:23 +0200
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <sjora4$2vo$1@dont-email.me>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
<n2i03i-vg.ln1@ID-313840.user.individual.net>
<83v929gucn.fsf@helmutwaitzmann.news.arcor.de> <87h7dscw7k.fsf@bsb.me.uk>
<2sL7J.103836$jl2.15629@fx34.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Oct 2021 07:17:24 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="69a8d5f1aff5da6a309b0977b9e323cc";
logging-data="3064"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fvNUXj4XVv/v4F0wXFh93"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:JC6DGt6J7YpFzVUY4ieIc4E2yB8=
In-Reply-To: <2sL7J.103836$jl2.15629@fx34.iad>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Fri, 8 Oct 2021 07:17 UTC

On 08.10.2021 01:32, Branimir Maksimovic wrote:
> On 2021-10-07, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> Helmut Waitzmann <nn.throttle@xoxy.net> writes:
>>
>>> Geoff Clare <geoff@clare.See-My-Signature.invalid>:
>>>
>>>> A typical use of this is to filter stderr:
>>>>
>>>> some_command 2>&1 >output.txt | some_filter >&2
>>>>
>>>> The 2>&1 makes stderr go to the pipe.
>>>>
>>>
>>> It's even possible to filter stdout as well as stderr, each of them by a
>>> filter of its own, for example:
>>>
>>> { { some_command 3>&- 4>&- | sed -e 's/^/stdout: /' 1>&3 3>&- 2>&4 4>&- }
>>> 2>&1 | sed -e 's/^/stderr: /' 1>&2 4>&- 3>&- } 3>&1 4>&2
>>
>> Wow. I take my hat off to you. I knew this was possible, I don't think I'd
>> be able to work out how. But it is an illustration of design failure. This
>> should not be hard.
>>
>> (When I need to do this, I used a named pipe.)
>>
> It's easy to read, it' just not common usage :P

Per design the feature operates on a very low abstraction level. Of
course you can try to reconstruct the individual pieces, redirecting,
duplicating, closing, on the various bracketed levels, but it's not
obvious and also error prone. On the other hand that allows to solve
most (all?) redirection tasks. Above solution is best taken as fixed
code pattern so that you don't have to rebuild that cryptic expression
every time you need it, and an attached code comment helps to remember
what it does without the need to re-confirm its functionality.

Janis

Re: How to redirect to non-standard file descriptors?

<3261a2fa-a087-45ab-871d-448b99645f55n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4466&group=comp.unix.shell#4466

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a37:444d:: with SMTP id r74mr2081669qka.405.1633689050164; Fri, 08 Oct 2021 03:30:50 -0700 (PDT)
X-Received: by 2002:ac8:6b96:: with SMTP id z22mr10635586qts.311.1633689049895; Fri, 08 Oct 2021 03:30:49 -0700 (PDT)
Path: rocksolid2!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Fri, 8 Oct 2021 03:30:49 -0700 (PDT)
In-Reply-To: <83v929gucn.fsf@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=78.165.8.188; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 78.165.8.188
References: <irtteiFpfsqU1@mid.individual.net> <sr0t2i-cjm2.ln1@wilbur.25thandClement.com> <87mtnn8ewy.fsf@nosuchdomain.example.com> <n2i03i-vg.ln1@ID-313840.user.individual.net> <83v929gucn.fsf@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3261a2fa-a087-45ab-871d-448b99645f55n@googlegroups.com>
Subject: Re: How to redirect to non-standard file descriptors?
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Fri, 08 Oct 2021 10:30:50 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 22
 by: Oğuz - Fri, 8 Oct 2021 10:30 UTC

On Thursday, October 7, 2021 at 10:37:48 AM UTC+3, Helmut Waitzmann wrote:
> Geoff Clare <ge...@clare.See-My-Signature.invalid>:
> >A typical use of this is to filter stderr:
> >
> >some_command 2>&1 >output.txt | some_filter >&2
> >
> >The 2>&1 makes stderr go to the pipe.
> >
> It's even possible to filter stdout as well as stderr, each of them
> by a filter of its own, for example:
>
> {
> { some_command 3>&- 4>&- |
> sed -e 's/^/stdout: /' 1>&3 3>&- 2>&4 4>&-
> } 2>&1 |
> sed -e 's/^/stderr: /' 1>&2 4>&- 3>&-
> } 3>&1 4>&2

How is this any different from

{ { some_command | sed -e 's/^/stdout: /' >&3 2>&4; } 2>&1 | sed -e 's/^/stderr: /' >&2; } 3>&1 4>&2

? What is the point of closing 3 and 4?

Re: How to redirect to non-standard file descriptors?

<83v925g3be.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4467&group=comp.unix.shell#4467

  copy link   Newsgroups: comp.unix.shell
Path: rocksolid2!i2pn.org!aioe.org!TKaXWw1jQ9d5mvvIPOjm4g.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: How to redirect to non-standard file descriptors?
Date: Sun, 10 Oct 2021 01:56:37 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83v925g3be.fsf@helmutwaitzmann.news.arcor.de>
References: <irtteiFpfsqU1@mid.individual.net>
<sr0t2i-cjm2.ln1@wilbur.25thandClement.com>
<87mtnn8ewy.fsf@nosuchdomain.example.com>
<n2i03i-vg.ln1@ID-313840.user.individual.net>
<83v929gucn.fsf@helmutwaitzmann.news.arcor.de>
<3261a2fa-a087-45ab-871d-448b99645f55n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="44127"; posting-host="TKaXWw1jQ9d5mvvIPOjm4g.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
X-Notice: Filtered by postfilter v. 0.9.2
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:O/mk9sxPYiX5IFqd5NN5drQJ2n8=
 by: Helmut Waitzmann - Sat, 9 Oct 2021 23:56 UTC

Oğuz <oguzismailuysal@gmail.com>:
>On Thursday, October 7, 2021 at 10:37:48 AM UTC+3, Helmut Waitzmann wrote:
>> Geoff Clare <ge...@clare.See-My-Signature.invalid>:
>> >A typical use of this is to filter stderr:
>> >
>> >some_command 2>&1 >output.txt | some_filter >&2
>> >
>> >The 2>&1 makes stderr go to the pipe.
>> >
>> It's even possible to filter stdout as well as stderr, each of
>> them by a filter of its own, for example:
>>
>> {
>> { some_command 3>&- 4>&- |
>> sed -e 's/^/stdout: /' 1>&3 3>&- 2>&4 4>&-
>> } 2>&1 |
>> sed -e 's/^/stderr: /' 1>&2 4>&- 3>&-
>> } 3>&1 4>&2
>
>How is this any different from
>
>{ { some_command | sed -e 's/^/stdout: /' >&3 2>&4; } 2>&1 | sed -e 's/^/stderr: /' >&2; } 3>&1 4>&2
>
>?

Using "1>&3" rather than ">&3" makes it for me more explicit which
file descriptor table entry is copied, but both forms are
equivalent.

>What is the point of closing 3 and 4?
>

File descriptor hygiene.  When not closing 3 and 4, "some_command"
might[1] be able to write to the file descriptors 3 and 4, thus
circumventing the filters and causing unfiltered output to the
invoker's file descriptors 1 and 2.  Depending on the invocation
environment this might cause a security hole.

[1] Whether "some_command" actually will be able to write to the
file descriptors 3 and 4 depends on the implementation of the
(POSIX) shell:  Iirc POSIX neither mandates nor prohibits the
setting of the close‐on‐exec flag on the file descriptors 3 and 4 by
the shell.

And even if "some_command" is not misbehaving, the unnecessarily
open file descriptors 3 and 4 will decrease the number of available
file descriptors for "some_command" and for the "sed" filters.


devel / comp.unix.shell / How to redirect to non-standard file descriptors?

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor