计算字符长度的方法

定义一个变量aaa

1 [root@rhel7-1 test]# aaa="I am a chairman"
2 [root@rhel7-1 test]# echo $aaa
3 I am a chairman
4 [root@rhel7-1 test]# echo ${aaa}
5 I am a chairman

实现计算的四种方法:

 1 方法一:
 2 [root@rhel7-1 test]# echo ${#aaa}
 3 15
 4 方法二:
 5 [root@rhel7-1 test]# expr length "$aaa"
 6 15
 7 方法三:
 8 [root@rhel7-1 test]# echo ${aaa} | wc -L
 9 15
10 方法四:
11 [root@rhel7-1 test]# echo ${aaa} | awk '{print length ($0)}'
12 15

 

简单shell小例子:利用for循环打印字符小于7的单词

 1 [root@rhel7-1 test]# cat word_length.sh
 2 #!/bin/bash
 3 #打印字符小于7的单词
 4 
 5 aaa="Dont forget a persons greatest emotional need is to feel appreciated"
 6 for i in $aaa
 7 do
 8         if [ `expr length $i` -le 6 ]
 9         then
10                 echo $i
11         fi
12 done

 

转载于:https://www.cnblogs.com/zhoul/p/9924083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值