aboutsummaryrefslogtreecommitdiff
path: root/doc/usage/cmd/if.rst
blob: 6b3dbe7b0a050aca443d66896b65d1becff1011c (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
.. SPDX-License-Identifier: GPL-2.0-or-later

.. index::
   single: if (command)

if command
==========

Synopsis
--------

::

    if <test statement>
    then
        <statements>
    fi

    if <test statement>
    then
        <statements>
    else
        <statements>
    fi

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

The if command is used to conditionally execute statements.

test statement
    Any command. The test statement set the $? variable. If the value of
    $? becomes 0 (true) the statements after the **then** statement will
    be executed. Otherwise the statements after the **else** statement.

Example
-------

The examples shows how the value of a numeric variable can be tested with
**itest**.

::

    => a=1; if itest $a == 0; then echo true; else echo false; fi
    false
    => a=0; if itest $a == 0; then echo true; else echo false; fi
    true

In the following example we try to load an EFI binary via TFTP. If loading
succeeds, the binary is executed.

::

    if tftp $kernel_addr_r shellriscv64.efi; then bootefi $kernel_addr_r; fi

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

The value of $? is the return value of the last executed statement.

::

    => if true; then true; else true; fi; echo $?
    0
    => if false; then true; else true; fi; echo $?
    0
    => if false; then false; else false; fi; echo $?
    1
    => if true; then false; else false; fi; echo $?
    1
    => if false; then true; fi; echo $?
    1