1. 字符串长度
echo ${#str}
2. 字符串转为数字形式计算
echo $((str-1))
3. 截取指定长度字符串 (下标从 0 开始)
echo ${str:0:3}
4. 字符串比较
4-1.
if [ $str != "" ]; then
# if [ $str = "" ]; then
echo $str
fi
4-2.
test "abc" = "abc" ; echo $? # 0
test "abc" != "abc" ; echo $? # 1
4-3. 字符串长度的判断
echo ${#str}
2. 字符串转为数字形式计算
echo $((str-1))
3. 截取指定长度字符串 (下标从 0 开始)
echo ${str:0:3}
4. 字符串比较
4-1.
if [ $str != "" ]; then
# if [ $str = "" ]; then
echo $str
fi
4-2.
test "abc" = "abc" ; echo $? # 0
test "abc" != "abc" ; echo $? # 1
4-3. 字符串长度的判断
[ "" ]; echo $? #1
[ "a" ]; echo $? # 0
test -z ""; echo $? # 0
test -z "a"; echo $? # 1
test -n ""; echo $? # 1
test -n "a"; echo $? # 0
5. 字符串连结
str1="a"
str2="b"
str=$str1$str2"c"
echo $str
6. 设置默认值
变量 AAA 未设置时, 使用默认值, 设置时, 使用设置的值
#!bin/sh
#AAA="world"
BBB=${AAA:-"hello"}
echo "AAA="$AAA # 空
echo "BBB="$BBB # hello
AAA="world"
BBB=${AAA:-"hello"}
echo "AAA="$AAA # world
echo "BBB="$BBB # world
test -z ""; echo $? # 0
test -z "a"; echo $? # 1
test -n ""; echo $? # 1
test -n "a"; echo $? # 0
5. 字符串连结
str1="a"
str2="b"
str=$str1$str2"c"
echo $str
6. 设置默认值
变量 AAA 未设置时, 使用默认值, 设置时, 使用设置的值
#!bin/sh
#AAA="world"
BBB=${AAA:-"hello"}
echo "AAA="$AAA # 空
echo "BBB="$BBB # hello
AAA="world"
BBB=${AAA:-"hello"}
echo "AAA="$AAA # world
echo "BBB="$BBB # world
0 件のコメント:
コメントを投稿