shell的for循環

for語法

for 變量 in 元素
do
作什麼
doneshell

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

 

知道每一個單詞的長度是多少spa

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

 

如何作比較 取出單詞長度小於3code

[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
相關文章
相關標籤/搜索