我有一个函数,想使用pipefail
里面的选项。但我不想简单地set -o pipefail
,因为我担心脚本的另一部分可能不会pipefail
被设置。当然我可以set +o pipefail
事后做,但如果pipefail
设置在我的函数之外,这也可能会引入意外行为。
当然false | true
,如果pipefail
设置了,我可以使用退出代码作为衡量标准,但这似乎有点脏。
有没有更通用的(也许是规范的?)方法来检查设置的 bash 选项吗?
是否可以使用case
?检查变量是否包含在数组中?我想做类似的事情
ARR=( opt1 opt2 opt3 );
case $1 in
$ARR)
echo "Option is contained in the array";
*)
echo "Option is not contained in the array";
esac
Run Code Online (Sandbox Code Playgroud)