gpt4 book ai didi

grep 结果的 Bash 条件 'OR'

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

我正在尝试在 bash 脚本中对两个 grep 命令的结果进行条件检查,但我似乎无法像这样将它们组合起来:

if [[ $( stout_cmd1 | grep -q 'check_txt1' ) || ( $( stdout_cmd2 | grep -q 'check_txt2' ) ]] ; then
//do something here if output of stdout_cmdX contains text check_txtX
fi

因为它总是返回 false。按预期有条件地检查自己工作的命令:

if stdout_cmd1 | grep -q 'check_txt1' ; then

if stdout_cmd2 | grep -q 'check_txt2' ; then

我不知道如何检查其中一个是否为真。如何在条件检查中组合两个标准输出输出?我已从 grep 调用中删除了 -q 标志,但没有任何效果。

最佳答案

实际上你只是在要求它运行静默-q之后直接使用grepexit-code

if cmd1 | grep -q "check_txt1" || cmd2 | grep -q "check_txt2"
then
echo "Your code here!"
fi

关于grep 结果的 Bash 条件 'OR',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42021964/

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