setexpr.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. setexpr command
  3. ===============
  4. Synopsis
  5. --------
  6. ::
  7. setexpr[.b, .w, .l .s] <name> [*]<value> <op> [*]<value2>
  8. setexpr[.b, .w, .l] <name> [*]<value>
  9. setexpr <name> fmt <format> [value]...
  10. setexpr <name> gsub r s [t]
  11. setexpr <name> sub r s [t]
  12. Description
  13. -----------
  14. The setexpr command is used to set an environment variable to the result
  15. of an evaluation.
  16. setexpr[.b, .w, .l .s] <name> [*]<value> <op> [*]<value2>
  17. Set environment variable <name> to the result of the evaluated
  18. expression specified by <op>.
  19. setexpr[.b, .w, .l] name [*]value
  20. Load <value> into environment variable <name>
  21. setexpr name fmt <format> value
  22. Set environment variable <name> to the result of the C like
  23. format string <format> evaluation of <value>.
  24. setexpr name gsub <r> <s> [<t>]
  25. For each substring matching the regular expression <r> in the
  26. string <t>, substitute the string <s>.
  27. The result is assigned to <name>.
  28. If <t> is not supplied, use the old value of <name>.
  29. setexpr name sub <r> <s> [<t>]
  30. Just like gsub(), but replace only the first matching substring
  31. The setexpr command takes the following arguments:
  32. format
  33. This parameter contains a C or Bash like format string.
  34. The number of arguments is limited to 4.
  35. The following format types are supported:
  36. c
  37. single character
  38. d, i
  39. decimal value
  40. o
  41. octal value
  42. s
  43. string
  44. u
  45. unsigned decimal value
  46. x, X
  47. hexadecimal value
  48. '%'
  49. no conversion, instead a % character will be written
  50. Backslash escapes:
  51. \" = double quote
  52. \\ = backslash
  53. \a = alert (bell)
  54. \b = backspace
  55. \c = produce no further output
  56. \f = form feed
  57. \n = new line
  58. \r = carriage return
  59. \t = horizontal tab
  60. \v = vertical tab
  61. \NNN = octal number (NNN is 0 to 3 digits)
  62. name
  63. The name of the environment variable to be set
  64. op
  65. '|'
  66. name = value | value2
  67. '&'
  68. name = value & value2
  69. '+'
  70. name = value + value2
  71. (This is the only operator supported for strings.
  72. It acts as concatenation operator on strings)
  73. '^'
  74. name = value ^ value2
  75. '-'
  76. name = value - value2
  77. '*'
  78. name = value * value2
  79. '/'
  80. name = value / value2
  81. '%'
  82. name = value % value2
  83. r
  84. Regular expression
  85. s
  86. Substitution string
  87. t
  88. string
  89. value
  90. Can either be an integer value, a string.
  91. If the pointer prefix '*' is given value is treated as memory address.
  92. value2
  93. See value
  94. Example
  95. -------
  96. ::
  97. => setexpr foo fmt %d 0x100
  98. => echo $foo
  99. 256
  100. =>
  101. => setexpr foo fmt 0x%08x 63
  102. => echo $foo
  103. 0x00000063
  104. =>
  105. => setexpr foo fmt %%%o 8
  106. => echo $foo
  107. %10
  108. =>
  109. Configuration
  110. -------------
  111. The setexpr gsub and sub operations are only available if CONFIG_REGEX=y.
  112. Return value
  113. ------------
  114. The return value $? is set to 0 (true) if the operation was successful.
  115. If an error occurs, the return value $? is set to 1 (false).