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