Shell中的expr命令

版权声明:自由转载,请附链接 https://blog.csdn.net/kangkanglou/article/details/83409366

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..."

执行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kangkanglou/article/details/83409366
今日推荐