#!/bin/bash
#==================================================
#LenOfStr.sh
#This script can calculate the length of strings.
#==================================================
#Version 1.0 \2009-11-09\by zhengyu
#Any questions can be emailed to
#==================================================
ERORR_NUM=100
if [[ -z $1 ]]
then
echo -e "Usage:`basename $0` string"
echo -e "Example:`basename $0` abc"
exit $ERORR_NUM
else
echo -e "There are $# strings"
until [[ -z $1 ]]
do
echo "=========================================================="
echo -e "We'll use two methods to calculate the length of \"$1\""
echo 'First Method:expr length $1'
echo -e "Result:The length of \"$1\" is `expr length $1`"
echo 'Second Method:expr "$1" : '.*''
echo -e "Result:The length of \"$1\" is `expr "$1" : '.*'`"
shift
done
exit 0
fi
在这个程序中,复习了一下shift的使用方法,学习了计算一个字符串长度的两种方法。我看书的时候,书上说还有一种方法是${#string},但是我在实际中测试的时候,该方法并不能得出正确的答案,我的系统是red hat enterprise Linux 4。不知道其他版本的Linux行不行。
还有一个疑问就是:"exit 0"是应该放在done的后面还是fi的后面。如果有谁知道答案请告诉我,谢谢!
阅读(967) | 评论(0) | 转发(0) |