-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbashconditionals
More file actions
executable file
·75 lines (63 loc) · 1.67 KB
/
bashconditionals
File metadata and controls
executable file
·75 lines (63 loc) · 1.67 KB
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
#!/bin/bash
echo "Control execution, evaluating [ 1 -eq 0 ], which is not true:"
if [ 1 -eq 0 ]; then
echo "The expression evaluates as true."
else
echo "The expression evaluates as false."
fi
# In the following examples, I unset boolval before setting it with
# a new statement to avoid carryover from a previous experiment
# when a statement has no effect on the variable.
echo
echo "Experiment 1: Setting boolval=false, boolval should be false."
unset boolval
boolval=false
echo -n "boolval is '$boolval' "
if [ boolval ]; then
echo "boolval evaluates true"
else
echo "boolval evaluates false"
fi
echo
echo "Experiment 2: Setting boolval=1, boolval should be false."
unset boolval
boolval=1
echo -n "boolval is '$boolval' "
if [ boolval ]; then
echo "boolval evaluates true"
else
echo "boolval evaluates false"
fi
echo
echo "Experiment 3: Setting boolval=\$( [ 1 -eq 0 ] ), boolval should be false."
unset boolval
boolval=$( [ 1 -eq 0 ] )
echo -n "boolval is '$boolval' "
if [ boolval ]; then
echo "boolval evaluates true"
else
echo "boolval evaluates false"
fi
echo
echo "Experiment 4: Setting boolval=\$( [ 1 -eq 0 ]; echo \$? ), boolval should be false."
unset boolval
boolval=$( [ 1 -eq 0 ]; echo $? )
echo -n "boolval is '$boolval' "
if [ boolval ]; then
echo "boolval evaluates true"
else
echo "boolval evaluates false"
fi
echo
echo "None of the experiments resulted in a variable that directly evaluates"
echo "as false. Evaluating a variable will "
loop_with_lambda()
{
local -i limit=10
local -i ndx=0
keep_looping() { (( ndx++ )); [ "$ndx" -le "$limit" ]; }
while keep_looping; do
echo "ndx = $ndx"
done
}
loop_with_lambda