方式一:
arg="$1"
#get length of first argument
$leng
while [ -n "$arg" ]; do
leng=`expr length "$1"`
echo "length is $leng"
break;
done
方式二:
#!/bin/bash
#by whuang 2012-06-23
if [ -z $1 ];then
echo "sorry,no arg"
else
leng=`expr length "$1"`
echo "length is $leng"
fi
方式三:
#!/bin/bash
#by whuang 2012-06-23
[ -z $1 ] && (echo "sorry,no arg";
echo "Please give an argument .")
[ ! -z $1 ] && (leng=`expr length "$1"`
echo "The length is $leng")
(2)拆分第一个参数
问题:拆分whuang中的hua
答案:expr substr "whuang" 2 3
说明:后面的两个参数2 和3,分别表示起点和长度
(3)获取指定index上的单个字符:
expr substr "whuang" 2 1
或:
echo abc|cut -b 2
echo abc|cut -b 3
(4)判断指定字符在字符串中的index:
expr index "whuang" u