偶然在网上看到一道测试题,貌似是某家公司的面试题,要求如下
使用shell判断abc test hello world this important life中字母超过4个的字符串
1
2
3
4
5
6
7
8
#!/bin/bash
for i in abc test hello world this important life
do
if [ `echo $i|wc -L` -gt 4 ]
then
echo $i
fi
done
输出结果如下:
1
2
3
4
test@test:~$ /bin/sh test.sh
hello
world
important
这里我们可以使用wc -L来判断字符串的长度,例如:
echo abcderf|wc -L
7
1
2
echo abcderf|wc -L
7
阅读(3303) | 评论(0) | 转发(0) |