summaryrefslogtreecommitdiff
path: root/doc/usage/cmd/setexpr.rst
blob: d245a13ca88b5300ab83197679de052515af5941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
.. SPDX-License-Identifier: GPL-2.0+

.. index::
   single: setexpr (command)

setexpr command
===============

Synopsis
--------

::

    setexpr[.b, .w, .l .s] <name> [*]<value> <op> [*]<value2>
    setexpr[.b, .w, .l] <name> [*]<value>
    setexpr <name> fmt <format> [value]...
    setexpr <name> gsub r s [t]
    setexpr <name> sub r s [t]

Description
-----------

The setexpr command is used to set an environment variable to the result
of an evaluation.

setexpr[.b, .w, .l .s] <name> [*]<value> <op> [*]<value2>
     Set environment variable <name> to the result of the evaluated
     expression specified by <op>.

setexpr[.b, .w, .l] name [*]value
     Load <value> into environment variable <name>

setexpr name fmt <format> value
     Set environment variable <name> to the result of the C like
     format string <format> evaluation of <value>.

setexpr name gsub <r> <s> [<t>]
     For each substring matching the regular expression <r> in the
     string <t>, substitute the string <s>.
     The result is assigned to <name>.
     If <t> is not supplied, use the old value of <name>.

setexpr name sub <r> <s> [<t>]
     Just like gsub(), but replace only the first matching substring

The setexpr command takes the following arguments:

format
    This parameter contains a C or Bash like format string.
    The number of arguments is limited to 4.
    The following format types are supported:

    c
        single character
    d, i
        decimal value
    o
        octal value
    s
        string
    u
        unsigned decimal value
    x, X
        hexadecimal value
    '%'
        no conversion, instead a % character will be written

    Backslash escapes:

    \" = double quote
    \\ = backslash
    \a = alert (bell)
    \b = backspace
    \c = produce no further output
    \f = form feed
    \n = new line
    \r = carriage return
    \t = horizontal tab
    \v = vertical tab
    \NNN = octal number (NNN is 0 to 3 digits)

name
    The name of the environment variable to be set

op
    '|'
        name = value | value2
    '&'
        name = value & value2
    '+'
        name = value + value2
        (This is the only operator supported for strings.
	It acts as concatenation operator on strings)
    '^'
        name = value ^ value2
    '-'
        name = value - value2
    '*'
        name = value * value2
    '/'
        name = value / value2
    '%'
        name = value % value2

r
    Regular expression

s
    Substitution string

t
    string

value
    Can either be an integer value, a string.
    If the pointer prefix '*' is given value is treated as memory address.

value2
    See value

Example
-------

::

    => setexpr foo fmt %d 0x100
    => echo $foo
    256
    =>

    => setexpr foo fmt 0x%08x 63
    => echo $foo
    0x00000063
    =>

    => setexpr foo fmt %%%o 8
    => echo $foo
    %10
    =>

Configuration
-------------

* The *setexpr* command is only available if CMD_SETEXPR=y.
* The *setexpr fmt* sub-command is only available if CMD_SETEXPR_FMT=y.
* The *setexpr gsub* and *setexpr sub* sub-commands are only available if
  CONFIG_REGEX=y.

Return value
------------

The return value $? is set to 0 (true) if the operation was successful.

If an error occurs, the return value $? is set to 1 (false).