blob: 813f903a8d833748f94f0b58232726135a5723ad [file] [log] [blame]
Heinrich Schuchardta9122d22024-03-16 10:57:41 +01001.. SPDX-License-Identifier: GPL-2.0-or-later
2
3.. index::
4 single: if (command)
5
6if command
7==========
8
9Synopsis
10--------
11
12::
13
14 if <test statement>
15 then
16 <statements>
17 fi
18
19 if <test statement>
20 then
21 <statements>
22 else
23 <statements>
24 fi
25
26Description
27-----------
28
29The if command is used to conditionally execute statements.
30
31test statement
32 Any command. The test statement set the $? variable. If the value of
33 $? becomes 0 (true) the statements after the **then** statement will
34 be executed. Otherwise the statements after the **else** statement.
35
Heinrich Schuchardtd2fbe1f2024-03-22 08:57:31 +010036Examples
37--------
Heinrich Schuchardta9122d22024-03-16 10:57:41 +010038
39The examples shows how the value of a numeric variable can be tested with
Heinrich Schuchardtd2fbe1f2024-03-22 08:57:31 +010040the :doc:`itest <itest>` command.
Heinrich Schuchardta9122d22024-03-16 10:57:41 +010041
42::
43
44 => a=1; if itest $a == 0; then echo true; else echo false; fi
45 false
46 => a=0; if itest $a == 0; then echo true; else echo false; fi
47 true
48
49In the following example we try to load an EFI binary via TFTP. If loading
50succeeds, the binary is executed.
51
52::
53
54 if tftp $kernel_addr_r shellriscv64.efi; then bootefi $kernel_addr_r; fi
55
56Return value
57------------
58
59The value of $? is the return value of the last executed statement.
60
61::
62
63 => if true; then true; else true; fi; echo $?
64 0
65 => if false; then true; else true; fi; echo $?
66 0
67 => if false; then false; else false; fi; echo $?
68 1
69 => if true; then false; else false; fi; echo $?
70 1
71 => if false; then true; fi; echo $?
72 1