[英]How does: `if ls /etc/*release 1>/dev/null 2>&1` work? An explanation please
Could someone help me under stand the condition ls /etc/*release 1>/dev/null 2>&1
that's contained in the code: 有人可以帮我保持条件
ls /etc/*release 1>/dev/null 2>&1
包含在代码中:
if ls /etc/*release 1>/dev/null 2>&1; then
echo "<h2>System release info</h2>"
echo "<pre>"
for i in /etc/*release; do
# Since we can't be sure of the
# length of the file, only
# display the first line.
head -n 1 $i
done
uname -orp
echo "</pre>"
fi
I pretty much don't understand any of that line but specifically what I wanted to know was: 我几乎不了解这一行,但具体是我想知道的是:
[ expression ]
? [ expression ]
? 1>/dev/null
a variable in the ls
statement? ls
语句中的变量是1>/dev/null
吗? 2>&1
? 2>&1
? I understand the purpose of this statement, which is; 我理解这句话的目的是, if there exists a file with release in it's name under the
/etc/
directory the statement will continue, I just don't understand how this achieves this. 如果在
/etc/
目录下存在一个带有release的文件,那么该语句将继续,我只是不明白这是如何实现的。
Thanks for you help 谢谢你的帮助
[
isn't a special character, it's a command ( /bin/[
or /usr/bin/[
, usually a link to test
). [
不是特殊字符,它是一个命令( /bin/[
或/usr/bin/[
,通常是test
的链接)。 That means 这意味着
if [ ...
if test ...
are the same. 是相同的。 For this to work,
test
ignores ]
as last argument if it's being called [
. 为了这个工作,
test
忽略]
作为最后一个参数,如果它被调用[
。
if
simply responds to the exit code of the command it invokes. if
只是响应它调用的命令的退出代码。 An exit code of 0 means success or "true". 退出代码0表示成功或“真”。
1>/dev/null 2>&1
redirects stdout ( 1
) to the device /dev/null
and then stderr ( 2
) to stdout which means the command can't display and output or errors on the terminal. 1>/dev/null 2>&1
将stdout( 1
)重定向到设备/dev/null
,然后将stderr( 2
)重定向到stdout,这意味着命令无法在终端上显示和输出或错误。
Since stdout isn't a normal file or device, you have to use >&
for the redirection. 由于stdout不是普通文件或设备,因此必须使用
>&
进行重定向。
At first glance, one would think that if [ -e /etc/*release ]
would be a better solution but test -e
doesn't work with patterns. 乍一看,人们会认为
if [ -e /etc/*release ]
是一个更好的解决方案,但test -e
不适用于模式。
The test
programm just evaluate its arguments and return a code 0 or 1 to tell whether it was true or not. test
程序只是评估它的参数并返回代码0或1来判断它是否为真。
But you can use any shell commands/function with a if
. 但是你可以使用任何shell命令/函数和
if
。 It will do the then
part if the return code ( $?
) was 0. So, here, we look if ls
return 0 (a file matched), or not. 如果返回码(
$?
)为0,它将执行then
部分$?
因此,在这里,我们看看ls
返回0(文件匹配),或者不是。
So, in the end, it's equivalent to write if [ -e /etc/*release ] ; then
所以,最后,它等同于写
if [ -e /etc/*release ] ; then
if [ -e /etc/*release ] ; then
, which is more "shell-liked". if [ -e /etc/*release ] ; then
,这更像是“贝壳喜欢”。
The last two statements 1>/dev/null
and 2>&1
are just here to avoid displaying the output of the ls
最后两个语句
1>/dev/null
和2>&1
就在这里是为了避免显示ls
的输出
1>/dev/null
redirect stdout to /dev/null
, so the standard out is not shown 1>/dev/null
将stdout重定向到/dev/null
,因此不显示标准输出 2>&1
redirect stderr to stdout. 2>&1
将stderr重定向到stdout。 Here, stdout is redirected to /dev/null
, so everything is redirected to /dev/null
/dev/null
,因此所有内容都被重定向到/dev/null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.