要求:编写一个脚本读入一些整数,分别输出这些整数中奇数的个数和偶数的个数,并输出0的个数.
实现脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#!/bin/bash #charpter_8-12 declare -a arr2
echo "Please input some integer numbers seprated by SPACE:"
read -a arr2
counter_odd=0 counter_even=0 counter_0=0 for i in "${arr2[@]}"
do if [[ i - ne 0 ]]
then
if [[ i%2 - eq 0 ]]
then
let counter_even+=1
else
let counter_odd+=1
fi
else
let counter_0+=1
fi
done echo "偶数个数:$counter_even"
echo "奇数个数:$counter_odd"
echo "0的个数:$counter_0"
|
脚本的执行效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost charpter8] # sh 8-12
Please input some integer numbers seprated by SPACE: 10 11 0 12 13 0 14 15 0 偶数个数:3 奇数个数:3 0的个数:3 [root@localhost charpter8] # sh 8-12
Please input some integer numbers seprated by SPACE: -12 -13 -14 0 12 13 14 15 0 17 偶数个数:4 奇数个数:4 0的个数:2 [root@localhost charpter8] #
|
可以看到准确的输出了结果.
本文转自 marbury 51CTO博客,原文链接:http://blog.51cto.com/magic3/1429696