bash 如果 ( ) < /dev/null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12363916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:15:33  来源:igfitidea点击:

if ( ) < /dev/null

bashunix

提问by Xu Wang

I can't wrap my head around this. Why would /dev/nullbe used as input to an ifstatement? What is the use of < /dev/nullin the following?

我无法解决这个问题。为什么会/dev/null被用作if语句的输入?以下有什么用< /dev/null

if ( $PROG --version ) < /dev/null > /dev/null 2>&1; then
        $PROG
else
        echo "failed"
        exit 1
fi

I (think) I understand that > /dev/null 2>&1is just used to suppress any output from both stdoutand stderr.

我(认为)我明白这> /dev/null 2>&1只是用来抑制来自stdout和 的任何输出stderr

采纳答案by Carl Norum

If $PROGis a program that expects input from stdin, your script might stall forever waiting for you to type something in. The use of < /dev/nullis to provide an empty input to such a program. You're right about the >and 2>&1.

如果$PROG是一个需要从 输入的程序stdin,则您的脚本可能会永远停止等待您输入内容。 的用途< /dev/null是向此类程序提供空输入。你是对的>2>&1

This snippet of script is checking to see if $PROG --versionexits with a 0 status (success), and then runs that program without any flags. If $PROG --versionfails, the script echoes "failed" and exits.

这段脚本正在检查是否$PROG --version以 0 状态(成功)退出,然后在没有任何标志的情况下运行该程序。如果$PROG --version失败,脚本会回显“失败”并退出。

回答by Keith Thompson

I think you're wondering why the redirection is outside the parentheses.

我想您想知道为什么重定向在括号之外。

In this line:

在这一行:

if ( $PROG --version ) < /dev/null > /dev/null 2>&1; then

the parentheses aren't part of the syntax of the ifstatement; they just specify command grouping. (It took me a moment to remember that myself; in csh/tcsh, parentheses arepart of the syntax of an ifstatement.)

括号不是if语句语法的一部分;他们只是指定命令分组。(我自己花了一点时间才记住这一点;在 csh/tcsh 中,括号if语句语法的一部分。)

For example, this:

例如,这个:

( echo one ; echo two ) | tr a-z A-Z

will produce this output:

将产生这个输出:

ONE
TWO

In this case, since $PROG --versionis a single command, the parentheses are unnecessary (unless $PROGexpands to more than one command, but that's unlikely).

在这种情况下,由于$PROG --version是单个命令,因此不需要括号(除非$PROG扩展为多个命令,但这不太可能)。

So the redirection doesn't apply to the ifstatement; it applies to $PROG --version. The purpose is to provide no input (as if reading from an empty file) to $PROG, and to discard anything it writes to stdout or stderr. If $PROGis a command that reads from stdin, even when invoked with --version, then without the input redirection it could hang waiting for keyboard input.

所以重定向不适用于if语句;它适用于$PROG --version. 目的是不向 提供输入(就像从空文件中读取一样)$PROG,并丢弃它写入 stdout 或 stderr 的任何内容。如果$PROG是从 stdin 读取的命令,即使使用 调用--version,如果没有输入重定向,它可能会挂起等待键盘输入。

The script assumes that it's safe to invoke $PROG(whatever it may be) only if $PROG --versiondoesn't produce an error.

该脚本假定$PROG只有在$PROG --version不产生错误的情况下才可以安全地调用(无论它是什么)。

Note that you canapply redirection to an if statement:

请注意,您可以将重定向应用于 if 语句:

if test_command ; then
    something
else
    something_else
fi < /dev/null > /dev/null 2>&1

This redirects input and output for test_command, something, andsomething_else.

这种重定向的输入和输出test_commandsomethingsomething_else