#!/bin/bash
#=====================================================
#operate_string.sh
#This script teach us some operations about strings.
#=====================================================
#Version 1.0\2009-11-10\by zhengyu
#Any question can be emailed to
#=====================================================
string=abcdeABCDEABCDE12345
# 123456789......
substring1=abc[A-Z]*
substring2=3E
echo "==============================================="
echo "Operation 1:match
expr match \"\$string\" '\$substring'"
echo "Result:"
echo -e " `expr match $string $substring1`"
echo "==============================================="
echo "Operation 2:index
expr match \"\$string\" '\$substring'"
echo "Result:"
echo -e " `expr index $string $substring2`"
echo "==============================================="
echo "Operation 3:
\${string:position}"
echo "Result:"
echo -e " ${string:2}"
echo -e " ${string:-2}"
echo -e " ${string:-default}"
echo -e " ${string:(-2)}"
echo -e " ${string: -2}"
echo "==============================================="
echo "Operation 4:
\${string:position:length}"
echo "Result:"
echo -e " ${string:2:4}"
echo -e " ${string:(-5):4}"
echo -e " ${string:-5:4)}"
echo -e " ${string:-5}"
echo -e " ${string:(-5):7}"
echo "==============================================="
echo "Operation 5:substr
expr substr \$string \$position \$length"
echo "Result:"
echo -e " `expr substr $string 2 5`"
echo "==============================================="
echo "Operation 6:
expr match "\$string" '\(\$substring\)'
expr "\$string" : '\(\$substring\)'"
echo "Result:"
echo -e " `expr match $string '\(.[a-z]*[A-Z]*[0-9]\)'`"
echo -e " `expr $string : '\(.[a-z]*[A-Z]*[0-9]\)'`"
echo -e " `expr $string : '.*\(.....\)'`"
echo "==============================================="
echo "Operation 7:
expr match "\$string" '.*\(\$substring\)'
expr "\$string" : '.*\(\$substring\)'"
echo "Result:"
echo -e " `expr match "$string" '.*\([a-z][A-Z][0-9]*\)'`"
echo -e " `expr $string : '.*\([a-z][A-Z][0-9]*\)'`"
echo -e " `expr $string : '.*\(...\)'`"
echo "==============================================="
echo "Operation 8:
\${string#substring}
\${string##substring}"
echo "Result:"
echo -e " ${string#a*E}"
echo -e " ${string##a*E}"
echo "==============================================="
echo "Operation 9:
\${string%substring}
\${string%%substring}"
echo "Result:"
echo -e " ${string%E*5}"
echo -e " ${string%%E*5}"
exit 0
运行结果:
===============================================
Operation 1:match
expr match "$string" '$substring'
Result:
3
===============================================
Operation 2:index
expr match "$string" '$substring'
Result:
10
===============================================
Operation 3:
${string:position}
Result:
cdeABCDEABCDE12345
abcdeABCDEABCDE12345
abcdeABCDEABCDE12345
45
45
===============================================
Operation 4:
${string:position:length}
Result:
cdeA
1234
abcdeABCDEABCDE12345
abcdeABCDEABCDE12345
12345
===============================================
Operation 5:substr
expr substr $string $position $length
Result:
bcdeA
===============================================
Operation 6:
expr match $string '\($substring\)'
expr $string : '\($substring\)'
Result:
abcdeABCDEABCDE1
abcdeABCDEABCDE1
12345
===============================================
Operation 7:
expr match $string '.*\($substring\)'
expr $string : '.*\($substring\)'
Result:
eA
eA
345
===============================================
Operation 8:
${string#substring}
${string##substring}
Result:
ABCDE12345
12345
===============================================
Operation 9:
${string%substring}
${string%%substring}
Result:
abcdeABCDEABCD
abcdeABCD
阅读(817) | 评论(0) | 转发(0) |