Shell中的expr命令
expr EXPRESSION
将EXPRESSION的值打印到标准输出
- 使用index命令
test="AaBbCcDdEeFfGg"
echo $(expr index $test A)
echo $(expr index $test D)
echo $(expr index $test Z)
- 使用substr命令
echo $(expr substr $test 2 5)
- 使用match命令
#test not start with a, return 0
echo $(expr match $test "a*")
#test start with A, return 1
echo $(expr match $test "A*")
#test end with g, return matched position 14
echo $(expr match $test ".*g$")
#test not end with G, return 0
echo $(expr match $test ".*G$")
echo $(expr match $test "\(.*g$\)")
echo $(expr match $test "\(A*\)")
代码段
/bin/bash
test="AaBbCcDdEeFfGg"
echo $(expr index $test A)
echo $(expr index $test D)
echo $(expr index $test Z)
echo $(expr substr $test 2 5)
#test not start with a, return 0
echo $(expr match $test "a*")
#test start with A, return 1
echo $(expr match $test "A*")
#test end with g, return matched position 14
echo $(expr match $test ".*g$")
#test not end with G, return 0
echo $(expr match $test ".*G$")
echo $(expr match $test "\(.*g$\)")
echo $(expr match $test "\(A*\)")
echo $(expr length $test)
echo $(expr 5 = 5)
echo "bye..."
执行结果