for语法

for 变量 in 元素
do
做什么
done

[root@shell ~]# cat for.sh 
#!/bin/sh
for i in I am ajie teacher I am 18
do
    echo $i
done

 

知道每个单词的长度是多少

[root@shell ~]# cat for.sh
#!/bin/sh
for i in I am lizhenya teacher I am 18
do
    echo ${#i}
done

 

如何做比较 取出单词长度小于3

[root@shell ~]# cat for.sh
#!/bin/sh
for i in I am lizhenya teacher I am 18
do
    [ ${#i} -lt 3 ] && echo $i
done
[root@shell ~]# sh for.sh
I
am
I
am
18    

 

[root@shell ~]# echo $name|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}'
I
am
I
am
18