Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本:如果条件的计算结果为真或假_Linux_Bash_Shell_Scripting - Fatal编程技术网

Linux Shell脚本:如果条件的计算结果为真或假

Linux Shell脚本:如果条件的计算结果为真或假,linux,bash,shell,scripting,Linux,Bash,Shell,Scripting,“if”关键字检查哪些环境变量或内部内容以确定正确/错误。 我有以下两种说法abc已装入,但未装入pqr if mount |grep -q "abc"; then echo "export pqr"; fi if mount |grep -q "pqr"; then echo "export abc"; fi 在上面的例子中,我希望第一条语句什么也不做,因为abc已装入(因此在mount o/p中查找行),因此mount | grep-q“abc”之后的$?为0。 我希望第二条语句执行ech

if
”关键字检查哪些环境变量或内部内容以确定正确/错误。 我有以下两种说法<代码>abc已装入,但未装入
pqr

if mount |grep -q "abc"; then echo "export pqr"; fi
if mount |grep -q "pqr"; then echo "export abc"; fi
在上面的例子中,我希望第一条语句什么也不做,因为
abc
已装入(因此在mount o/p中查找行),因此
mount | grep-q“abc”
之后的
$?
为0。 我希望第二条语句执行echo。但事实并非如此,第一条语句是打印,第二条语句不是。因此,我想了解判断正确/错误的依据是什么。

但这个问题的公认答案是:

if [ 0 ]
is equivalent to

if [ 1 ]

如果这是真的,那么我的两个陈述都应该做回显,对吗?

在您发出的命令和您从参考问题中得出的类比之间有一个基本的区别

当使用
-q
选项执行
grep
时,如果找到匹配项,它将退出,返回代码为零。这意味着,如果
mount
的输出包含
abc
,则

if mount |grep -q "abc"; then echo "export pqr"; fi
相当于说:

if true; then echo "export pqr"; fi
请注意,图中没有
test
命令,即
[


引述:

测试
[
内置函数使用一个集合来计算条件表达式 基于参数数量的规则

0个参数

The expression is false.
1参数

The expression is true if and only if the argument is not null.

这就解释了为什么
[0]
[1]
两者的计算结果都为真。

您发出的命令与您从参考问题中得出的类比之间存在基本的区别

if [ 0 ]
当使用
-q
选项执行
grep
时,如果找到匹配项,则返回代码为零。这意味着如果
mount
的输出包含
abc
,则

if mount |grep -q "abc"; then echo "export pqr"; fi
相当于说:

if true; then echo "export pqr"; fi
请注意,图中没有
test
命令,即
[


引述:

测试
[
内置函数使用一个集合来计算条件表达式 基于参数数量的规则

0个参数

The expression is false.
1参数

The expression is true if and only if the argument is not null.
这就解释了为什么
[0]
[1]
都计算为true

if [ 0 ]
测试字符串
0
是否为非空。它是非空的,因此测试成功。类似地,
if[1]
成功,因为字符串
1
为非空。
[/code>命令(也称为
test
)根据其参数返回值。类似地,grep返回值。
if
关键字使shell根据命令返回的值执行命令,但是前面加有
if
的命令的输出是不相关的

命令
test0
(相当于命令
[0]
返回值0。命令
test1
也返回值0。shell将0视为成功,因此执行
if
子句的命令

测试字符串
0
是否为非空。它是非空的,因此测试成功。类似地,
if[1]
成功,因为字符串
1
为非空。
[/code>命令(也称为
test
)根据其参数返回值。类似地,grep返回值。
if
关键字使shell根据命令返回的值执行命令,但是前面加有
if
的命令的输出是不相关的


命令
test0
(相当于命令
[0]
返回一个值0。命令
test1
也返回一个值0。shell将零视为成功,因此执行
if
子句的命令。

if
命令在整数的“布尔值”:它作用于随后命令的退出状态。在shell中,退出状态为0被视为成功,任何其他退出状态都是失败。如果
之后的命令以状态0退出,则为“真”

例如:

$ test -f /etc/passwd; echo $?
0
$ test -f /etc/doesnotexist; echo $?
1
$ if test -f /etc/passwd; then echo exists; else echo does not exist; fi
exists
$ if test -f /etc/doesnotexist; then echo exists; else echo does not exist; fi
does not exist

请注意,
[
[[
是(基本上)alias
测试

的命令,如果
命令没有像类C语言一样作用于“布尔值”整数的退出:它作用于后面命令的退出状态。在shell中,退出状态为0被视为成功,任何其他退出状态都是失败。如果后面的命令以状态0退出,则为“真”

例如:

$ test -f /etc/passwd; echo $?
0
$ test -f /etc/doesnotexist; echo $?
1
$ if test -f /etc/passwd; then echo exists; else echo does not exist; fi
exists
$ if test -f /etc/doesnotexist; then echo exists; else echo does not exist; fi
does not exist
请注意,
[
[[
是(基本上)别名
测试的命令