编写一个shell脚本来统计并输出长度不超过5个字符的单词
发布时间
阅读量:
阅读量
编写shell脚本以输出下列语句中字符数量少于6的词汇。
The hard part isn’t making the decision. It’s living with it.
思路:首先提取所有词汇,接着统计每个词汇的字符数量,随后逐一进行判断。
测定变量内容长度的常用方式共有四种:
[root@localhost myShell]# str=hello
- 利用变量自身提供的长度获取方式( echo ${#str})
[root@localhost myShell]# echo ${#str}
5
- 采用管道结合wc的处理方式
[root@localhost myShell]# echo $str |wc -L
5
采用expr内置的length函数进行操作
[root@localhost myShell]# expr length $str
5
采用awk内置的length函数实现方式
[root@localhost myShell]# echo $str | awk '{print length($0)}'
5
因此,提出以下几种可行的应对策略:
``
全部评论 (0)
还没有任何评论哟~
