Shell中的expr命令

àì夳堔傛蜴生んèń 2022-05-14 14:14 370阅读 0赞

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
    )“)

代码段

  1. /bin/bash
  2. test="AaBbCcDdEeFfGg"
  3. echo $(expr index $test A)
  4. echo $(expr index $test D)
  5. echo $(expr index $test Z)
  6. echo $(expr substr $test 2 5)
  7. #test not start with a, return 0
  8. echo $(expr match $test "a*")
  9. #test start with A, return 1
  10. echo $(expr match $test "A*")
  11. #test end with g, return matched position 14
  12. echo $(expr match $test ".*g$")
  13. #test not end with G, return 0
  14. echo $(expr match $test ".*G$")
  15. echo $(expr match $test "\(.*g$\)")
  16. echo $(expr match $test "\(A*\)")
  17. echo $(expr length $test)
  18. echo $(expr 5 = 5)
  19. echo "bye..."

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

发表评论

表情:
评论列表 (有 0 条评论,370人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux expr命令

    在Linuxshell命令中expr虽然不是很起眼,但是它的作用是非常大的。包含:四则运算和字符串的操作。 一、先说四则运算,在Shell中四则运算不能简简单单的加减乘除,需

    相关 Linux expr命令

    expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。