Debian Codegraph @

and-or.sh

raw symbols SCIP
1 #!/bin/bash
2
3 a=24
4 b=47
5
6 if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
7 then
8 echo "Test #1 succeeds."
9 else
10 echo "Test #1 fails."
11 fi
12
13 # ERROR: if [ "$a" -eq 24 && "$b" -eq 47 ]
14 #+ attempts to execute ' [ "$a" -eq 24 '
15 #+ and fails to finding matching ']'.
16 #
17 # Note: if [[ $a -eq 24 && $b -eq 24 ]] works.
18 # The double-bracket if-test is more flexible
19 #+ than the single-bracket version.
20 # (The "&&" has a different meaning in line 17 than in line 6.)
21 # Thanks, Stephane Chazelas, for pointing this out.
22
23
24 if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]
25 then
26 echo "Test #2 succeeds."
27 else
28 echo "Test #2 fails."
29 fi
30
31
32 # The -a and -o options provide
33 #+ an alternative compound condition test.
34 # Thanks to Patrick Callahan for pointing this out.
35
36
37 if [ "$a" -eq 24 -a "$b" -eq 47 ]
38 then
39 echo "Test #3 succeeds."
40 else
41 echo "Test #3 fails."
42 fi
43
44
45 if [ "$a" -eq 98 -o "$b" -eq 47 ]
46 then
47 echo "Test #4 succeeds."
48 else
49 echo "Test #4 fails."
50 fi
51
52
53 a=rhino
54 b=crocodile
55 if [ "$a" = rhino ] && [ "$b" = crocodile ]
56 then
57 echo "Test #5 succeeds."
58 else
59 echo "Test #5 fails."
60 fi
61
62 exit 0