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