gpt4 book ai didi

bash - SHELL - IF 语句中的 AND 操作

转载 作者:行者123 更新时间:2023-11-29 09:36:01 24 4
gpt4 key购买 nike

假设这些功能:

return_0() {
return 0
}

return_1() {
return 1
}

然后是下面的代码:

if return_0; then
echo "we're in" # this will be displayed
fi

if return_1; then
echo "we aren't" # this won't be displayed
fi

if return_0 -a return_1; then
echo "and here we're in again" # will be displayed - Why ?
fi

为什么我进入最后一个 if 语句?难道我们不应该使用那些 01 吗?

最佳答案

-atest命令的选项之一(也是[[[)。所以你不能单独使用 -a 。您可能想使用 &&,它是 AND 列表的控制运算符标记。

if return_0 && return_1; then ...

可以使用-a告诉test“和”两个不同的test表达式,比如

if test -r /file -a -x /file; then
echo 'file is readable and executable'
fi

但这等同于

if [ -r /file -a -x /file ]; then ...

这可能更具可读性,因为括号使表达式的 test 部分更清晰。

有关...的更多信息,请参阅 Bash 引用手册

关于bash - SHELL - IF 语句中的 AND 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55729300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com