linux - 检查bash中多个变量的输出

标签 linux bash shell scripting centos

现在我有多个命令在我的 bash 脚本中定义为变量,如下所示:

LSIBATTSTATE=`/var/lib/einarc/tools/lsi_megacli/cli -AdpBbuCmd -GetBbuStatus -aALL | grep 'Operational'`
LSIBATT=`/var/lib/einarc/tools/lsi_megacli/cli -AdpBbuCmd -GetBbuStatus -aALL | grep 'isSOHGood: Yes'`
LSIWB=`/var/lib/einarc/tools/lsi_megacli/cli -LDinfo -Lall -Aall | grep 'WriteBack'`
ADAPZMM=`einarc ad info | grep 'ZMM Optimal'`

这是其中的 4 个,但有可能增加更多。 (这适用于 RAID Controller )。

我想做的是找出如何检查所有这些变量的输出以获取返回结果。基本上,类似这样的事情不必为每个做一个 if/then 语句:

ps cax | grep httpd > /dev/null
  if [ $? -eq 0 ]; then
  <do some stuff>
fi

如果找到所需的输出,则脚本继续前进并忽略它。但是,如果找不到所需的输出(即返回结果为空),那么我希望它执行一个操作。我认为在 bash 中真正做到这一点的唯一方法是使用 for 循环,但我不确定这是否是最好或最有效的方法。另外,数组在这种情况下有用吗?对我来说似乎不是这样,但是在 bash 中我仍然有很多事情没有我想的那么好。

如有任何帮助,我们将不胜感激。

最佳答案

这会将所有结果放在一个数组中,然后遍历它以查找任何空结果。

results=("`/var/lib/einarc/tools/lsi_megacli/cli -AdpBbuCmd -GetBbuStatus -aALL | grep 'Operational'`" 
         "`/var/lib/einarc/tools/lsi_megacli/cli -AdpBbuCmd -GetBbuStatus -aALL | grep 'isSOHGood: Yes'`" 
         "`/var/lib/einarc/tools/lsi_megacli/cli -LDinfo -Lall -Aall | grep 'WriteBack'`
ADAPZMM=`einarc ad info | grep 'ZMM Optimal'`" )
all_succeeded=1
for result in "${results[@]}"; do
  if [ -z "$result" ]
  then echo Something failed.
       all_succeeded=0
       break
  fi
done

关于linux - 检查bash中多个变量的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17116734/

相关文章:

linux - 锁定 View 的配置规范(clearcase)

linux - 根据数据包类型拆分 TTY 设备

python - 导入错误 : No module named 'gdbm' occuring while using source ~/. bashrc

arrays - Python 的 all 等价于 bash 的是什么?

linux - 使用 SSH 运行 swapon 时找不到命令

linux - sed 用特殊字符替换数字,除了一行中的最后一个字符串

node.js - webpack安装 webpack@3.5.5 想要

linux - 没有设备树 blob 的 bootm

bash - 我可以使用 shell 通配符来选择跨两位数的文件名(例如,从 foo_1.jpg 到 foo_54.jpg)吗?

java Runtime.exec 运行 shell 脚本