什么是一个bash脚本来测试多个值的最有效方法 [英] What is the most efficient way to test multiple values in a bash script
问题描述
我要对三个变量进行测试。如果所有三个变量被设定为预期值的测试将通过。如果只有两个或者一个都那么测试将失败。我们的想法是,如果有任何的失败,则那些将被报告。我该怎么做最有效?
目前,我只能想将其设置为如果... 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屋!