我有一个如下所示的两个数组B和T包含一个字符串,该字符串是我从文件中提取的,其中B [0]是时间T [0]的输出,B [1]是时间T [[ 1],依此类推。我的任务是看到在特定的持续时间T输出B可以为“ 0.00”或“ 1.0”,其余时间为“ 0.00”,因此B的值只是示例它可以是'0.00'或'1.0'。那么有什么更好的方法可以使If语句最小化,或者可以使用其他方法?
B:
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.0 0.00 1.0 0.00 1.0 0.00 0.00 0.00 0.00 0.00
T:
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57
Code:
j=0
for i in ${T[@]}
do
if [ $i == '10' -o $i == '11' -o $i == '12' -o $i == '13' -o $i == '14' -o $i == '15' -o $i == '16' -o $i == '17' -o $i == '18' -o $i =='19' -o $i == '20' -o $i == '51' -o $i == '52' -o $i == '53' -o $i == '54' -o $i == '55'-o $i == '56' -o $i == '57' -o $i == '58' -o $i == '59' -o $i == '60' ]
then
if [ ${B[$j]} == '1.0' -o ${B[$j]} == '0.00' ]
then
echo "Passed"
fi
else
if [ ${B[$j]} == '0.00' ]
then
echo "Passed"
else
echo "Failed"
fi
fi
j=$(( $j + 1 ))
done
答案 0 :(得分:2)
或
#!/bin/bash
B=(0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.0 0.00 1.0 0.00 1.0 0.00 0.00 0.00 0.00 0.00)
T=(3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57)
Code:
j=0
for i in ${T[@]}; do
if [[ $i =~ ^(10|11|12|13|14|15|16|17|18|19|20|51|52|53|54|55|56|57|58|59|60)$ ]]
then
if [[ ${B[$j]} == '1.0' || ${B[$j]} == '0.00' ]]
then
echo "Passed T=$i B=${B[$j]}"
fi
else
if [[ ${B[$j]} == '0.00' ]]
then
echo "Passed T=$i B=${B[$j]}"
else
echo "Failed T=$i B=${B[$j]}"
fi
fi
((j++))
done
哪个给我输出
Passed T=3 B=0.00
Passed T=6 B=0.00
Passed T=9 B=0.00
Passed T=12 B=0.00
Passed T=15 B=0.00
Passed T=18 B=0.00
Passed T=21 B=0.00
Passed T=24 B=0.00
Passed T=27 B=0.00
Failed T=30 B=1.0
Passed T=33 B=0.00
Failed T=36 B=1.0
Passed T=39 B=0.00
Failed T=42 B=1.0
Passed T=45 B=0.00
Passed T=48 B=0.00
Passed T=51 B=0.00
Passed T=54 B=0.00
Passed T=57 B=0.00
答案 1 :(得分:1)
paste -d' ' <(printf "%s\n" "${T[@]}") <(printf "%s\n" "${B[@]}") \
| sed '
s/^\(1[0-9]|20|5[1-9]|60\) \(0.00\|1.0\)$/Passed/; t;
s/^[0-9]\+ 0.00$/Passed/; t;
s/.*/Failed/;
'
printf "%s\n" "${arr[@]}"
将每个数组成员打印在单独的行上3 0.00\n6 0.00\n
[0-9]|20|5[1-9]|60
,第二列是0.00
或1.0
,则用该行替换Passed
Passed
0.00
部分已取消。Failed
sed
中,如果前一个t
成功,则s
选项将分支到脚本结尾。tutorialspoints的实时版本。