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
相关文章
相关标签/搜索