什么是一个bash脚本来测试多个值的最有效方法 [英] What is the most efficient way to test multiple values in a bash script

查看:114
本文介绍了什么是一个bash脚本来测试多个值的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要对三个变量进行测试。如果所有三个变量被设定为预期值的测试将通过。如果只有两个或者一个都那么测试将失败。我们的想法是,如果有任何的失败,则那些将被报告。我该怎么做最有效?

目前,我只能想将其设置为如果... ELIF块的大规模串。

 如果[TEST1]&放大器;&安培; [TEST2]&放大器;&安培; [TEST3]
然后
#重复,直到结果true和false所有组合都用尽。

你会如何测试所有的组合,而无需创建八个不同的如果... ELIF 测试?

  test1的&放大器;&安培;测试2和放大器;&安培; TEST3
测试1&放大器;&安培; !TEST2和放大器;&安培; TEST3
测试1&放大器;&安培;测试2和放大器;&安培; !TEST3
测试1&放大器;&安培; !TEST2和放大器;&安培; !TEST3
!测试1&放大器;&安培;测试2和放大器;&安培; TEST3
!测试1&放大器;&安培; !TEST2和放大器;&安培; TEST3
!测试1&放大器;&安培;测试2和放大器;&安培; !TEST3
!测试1&放大器;&安培; !TEST2和放大器;&安培; !TEST3


解决方案

也许这可能是因为逐一检查三个变量一样简单?

 通= 1
[TEST1] || {回声test1的失败;通过= 0}
[TEST2] || {回声test2的失败;通过= 0}
[TEST3] || {回声TEST3失败;通过= 0}
[[$通过-eq 0]]&功放;&安培;回声其中一个测试失败

I need to run a test against three variables. If all three variables are set to the expected values the test will pass. If only two or one are then the test will fail. The idea is that if any of the fail then those will be reported. How can I do this most efficiently?

At the moment I can only think to set it up as a massive string of if...elif blocks.

if [[ test1 ]] && [[ test2 ]] && [[ test3 ]]
then
# repeat until all combinations of true and false results are exhausted.

How would you test all of the combinations without creating eight different if...elif tests?

test1 && test2 && test3
test1 && !test2 && test3
test1 && test2 && !test3
test1 && !test2 && !test3
!test1 && test2 && test3
!test1 && !test2 && test3
!test1 && test2 && !test3
!test1 && !test2 && !test3

解决方案

Perhaps this could be as simple as checking the three variables individually?

pass=1
[[ test1 ]] || { echo 'test1 failed'; pass=0 }
[[ test2 ]] || { echo 'test2 failed'; pass=0 }
[[ test3 ]] || { echo 'test3 failed'; pass=0 }
[[ $pass -eq 0 ]] && echo 'one of the tests failed'

这篇关于什么是一个bash脚本来测试多个值的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆