English 中文(简体)
为什么在 b子里把direct转向?子里?
原标题:Why redirect stdin inside a while read loop in bash?

考虑如下:<><>mple 文字:

#!/bin/sh

do_something() {
    echo $@
    return 1
}

cat <<EOF > sample.text
This is a sample text
It serves no other purpose
EOF

cat sample.text | while read arg1 arg2 arg3 arg4 arg5; do
    ret=0
    do_something "$arg1" "$sarg2" "$arg3" "$arg4" "$arg5" <&3 || ret=$?
done 3<&1

What is the purpose of redirecting stdout as input for filedescriptor 3? At least in Bash, it does not seem to make any difference if omitted. Does it have any effect if it is executed in any other shell than bash?

UPDATE

对于在什么地方这样做的人们来说,这是一种从Debian s > 加密碎克_start/code>。 说明。

最佳回答

这里的明确意图是防止从<代码>mple.读到<>。 越权越权越权越权。 如果你不看到与改头不符或没有改变方向的行为,那是因为do_someth 确实,在检测中从小部分读到。

如果您有<条码>、和<条码>,则从同一行文读到<条码>,则<条码>所消耗的任何内容。 其后的<代码>read ——当然,你在输入<编码>时有不合法的内容,从而造成种种后果,如试图使用坏加密钥匙(如果真的世界使用案例如<编码>>>>>> 加密/编码>),以及p.c。

cat sample.text | while read arg1 arg2 arg3 arg4 arg5; do
    ret=0
    do_something "$arg1" "$sarg2" "$arg3" "$arg4" "$arg5" <&3 || ret=$?
done 3<&1

现在,它 s——<代码>3&1与<代码>3<&0相比是一种不良做法,因为它没有假定停用也是可以用作投入的东西——但成功实现这一目标。


顺便提一下:

exec 3</dev/tty || exec 3<&0     ## make FD 3 point to the TTY or stdin (as fallback)

while read -a args; do           ## |- loop over lines read from FD 0
  do_something "${args[@]}" <&3  ## |- run do_something with its stdin copied from FD 3
done <sample.text                ## -> ...while the loop is run with sample.txt on FD 0

exec 3<&-                        ## close FD 3 when done.

It s a little more verbose, needing to explicitly close FD 3, but it means that our code is no longer broken if we re run with stdout attached to the write-only side of a FIFO (or any other write-only interface) rather than directly to a TTY.


至于这种做法的弊端,它非常常见。 See for example the following StackOverflow questions concerning it:

等等。

问题回答

如前所述,这涉及将溪流分开阅读,但一般格式比较简单。

First get rid of the useless cat to keep the the while loop in the scope of the main script. This enables the contents of the loop to interact with stdin, stdout & stderr as usual, without interference from the arg reading from the text file. E.g.:

while read arg1 arg2 arg3 arg4 arg5 <&3; do
    ret=0
    do_something "$arg1" "$sarg2" "$arg3" "$arg4" "$arg5" || ret=$?
done 3< sample.text

The read probably needs the -r flag to prevent backslash escape sequence interpretation. do_something then receives stdin (or stdout with <&1 like the example, if that s even possible or needed). If sample.txt should be a command s output, use process substitution like done 3< <(arg_generating_command).





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签