方法1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/bin/bash xcn=(i am xcn teacher welcome to xcn training class) for word in ${xcn[*]}
do if [ ${ #word} -le 6 ]
then
echo $word
fi
done 执行结果: [root@slave ~] # sh test2.sh
i am xcn to xcn class |
方法2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/bin/bash xcn=(i am xcn teacher welcome to xcn training class) for ((i=0;i<${ #xcn[*]};i++))
do if [ ${ #xcn[$i]} -le 6 ]
then
echo ${xcn[$i]}
fi
done 执行结果: [root@slave ~] # sh test3.sh
i am xcn to xcn class |
方法3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/bin/bash chars= "i am xcn teacher welcome to xcn training class"
for n in $chars
do if [ ${ #n} -le 6 ]
then
echo $n
fi
done 执行结果: [root@slave ~] # sh test4.sh
i am xcn to xcn class |
本文转自 baishuchao 51CTO博客,原文链接:http://blog.51cto.com/baishuchao/1944172