bash的开关情况相匹配多个条件 [英] bash switch case matching multiple conditions
问题描述
我运行开关例列号,我的列数可以为0 -50。当在循环中运行 - 他们得到了验证。现在,每个案例支持离散列号,我观察它的faiure。
例如:我的列号为10,这里是code -
I'm running switch case with column number, my column numbers can be 0 -50. When run in loop - they get validated. Now each case supports discrete column number and i observe it's faiure. Example: my column number is 10, and here is the code -
i=10
a=1
b=0.65
if [ "$a" != "$b" ] ; then
case $i in
[1]|[2]|[5]) echo "Not OK"; ;;
[9-10]|[12]) echo "may be ok"; ;;
*) echo "no clue - $i"; ;;
esac;
fi
我想到可能是好的,但没有得到线索 - 10
I expect "may be ok" but get no clue - 10
推荐答案
猛砸情况
不以数字范围内工作。 []
是外壳的图案。
Bash case
doesn't work with numbers ranges. []
is for shell patterns.
例如这种情况下, [1-3] 5 | 6)
将15或25或35或工作6
for instance this case [1-3]5|6)
will work for 15 or 25 or 35 or 6.
您code应该是这样的:
Your code should look like this:
i=10
a=1
b=0.65
if [ "$a" != "$b" ] ; then
case $i in
1|2|5) echo "Not OK"; ;;
9|10|12) echo "may be ok"; ;;
*) echo "no clue - $i"; ;;
esac;
fi
如果 I
可真正
与 9和10,那么你就需要与范围使用如果
(而不是情况)。
If i
can be real
between 9 and 10 then you'll need to use if
(instead of case) with ranges.
这篇关于bash的开关情况相匹配多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!