1 #!/bin/bash
2 #
3 # Test the if statement
4
5 ### If
6 if true; then
7 echo if
8 fi
9 # stdout: if
10
11 ### else
12 if false; then
13 echo if
14 else
15 echo else
16 fi
17 # stdout: else
18
19 ### elif
20 if (( 0 )); then
21 echo if
22 elif true; then
23 echo elif
24 else
25 echo else
26 fi
27 # stdout: elif
28
29 ### Long style
30 if [[ 0 -eq 1 ]]
31 then
32 echo if
33 echo if
34 elif true
35 then
36 echo elif
37 else
38 echo else
39 echo else
40 fi
41 # stdout: elif
42
43 # Weird case from bash-help mailing list.
44 #
45 # "Evaluations of backticks in if statements". It doesn't relate to if
46 # statements but to $?, since && and || behave the same way.
47
48 ### If empty command
49 if ''; then echo TRUE; else echo FALSE; fi
50 # stdout: FALSE
51 # status: 0
52
53 ### If subshell true
54 if `true`; then echo TRUE; else echo FALSE; fi
55 # stdout: TRUE
56 # status: 0
57
58 ### If subshell true WITH OUTPUT is different
59 if `sh -c 'echo X; true'`; then echo TRUE; else echo FALSE; fi
60 # stdout: FALSE
61 # status: 0
62
63 ### If subshell true WITH ARGUMENT
64 if `true` X; then echo TRUE; else echo FALSE; fi
65 # stdout: FALSE
66 # status: 0
67
68 ### If subshell false
69 if `false`; then echo TRUE; else echo FALSE; fi
70 # stdout: FALSE
71 # status: 0