shell求字符串长度

${#string} $string的长度

${string:position} $string从位置$position开始提取子串

${string:position:length} $string从位置$position开始提取长度为$length的子串

shell中,通过awk,sed,expr 等都可以实现,字符串上述操作。下面我们进行性能比较。

[chengmo@localhost ~]$ test='c:/windows/boot.ini'

[chengmo@localhost ~]$ time for i in $(seq 10000);do a=${#test};done;

real    0m0.173s

user    0m0.139s

sys     0m0.004s

[chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length $test);done; 

real    0m9.734s

user    0m1.628s

原文:

http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html

posted @ 2012-12-20 00:21  浪里飞  阅读(1139)  评论(0编辑  收藏  举报