gpt4 book ai didi

bash which OR operator to use - 管道 v 双管道

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

当我查看 bash 脚本代码时,有时会看到 |,有时会看到 ||,但我不知道哪个更可取。

我正在尝试做类似..

set -e;

ret=0 && { which ansible || ret=$?; }

if [[ ${ret} -ne 0 ]]; then
# install ansible here
fi

请告知在这种情况下首选哪个 OR 运算符。

最佳答案

| 根本不是 OR 运算符。不过,您可以使用||:

which ansible || {
true # put your code to install ansible here
}

这相当于一个if:

if ! which ansible; then
true # put your code to install ansible here
fi

顺便说一下——考虑养成使用type(shell 内置命令)而不是which(外部命令)的习惯。 type 既更快又能更好地理解 shell 行为:如果你有一个 ansible 命令,它是由调用真实命令的 shell 函数提供的, which 不知道它在那里,但是 type 会正确地检测到它可用。

关于bash which OR operator to use - 管道 v 双管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625521/

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