Skip to content

Commit f2cd686

Browse files
authored
Create bash_conditional_statements
1 parent e906e0d commit f2cd686

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

bash_conditional_statements

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env bash
2+
3+
VAR="variable"
4+
5+
# In each of the statements below, note the space between
6+
# the 'if' and the '[' as well as the space before the ']'
7+
# this spacing is REQUIRED!
8+
9+
# verify that $VAR equals 'variable'
10+
if [ $VAR == "variable" ]
11+
then
12+
13+
echo "You should see this text!"
14+
15+
fi
16+
17+
# this is the same IF statement.
18+
# The comma after the ']' is usee to separate the conditional from
19+
# the 'then' keyword, which is ecpected to be on a different line.
20+
if [ $VAR == "variable" ]; then
21+
22+
echo "You should see this text!"
23+
24+
fi
25+
26+
27+
# -------------------------------------
28+
29+
30+
# an ELSE statement can be added as well.
31+
if [ $VAR == "variable" ]; then
32+
33+
echo "You should see this text!"
34+
35+
else
36+
37+
echo "You should NOT see this text!"
38+
39+
fi
40+
41+
42+
# -------------------------------------
43+
44+
45+
# inequality can be tested as such.
46+
if [ $VAR != "variable" ]; then
47+
48+
echo "You should NOT see this text!"
49+
50+
else
51+
52+
echo "You should see this text!"
53+
54+
fi
55+
56+
57+
# -------------------------------------
58+
59+
60+
# Special flags can also be used. For example,
61+
# this conditional tests that the given variable
62+
# is an empty string.
63+
if [ -z $VAR ]; then # the -z stands for zero, as in zero content.
64+
65+
echo "You should NOT see this text!"
66+
67+
else
68+
69+
echo "You should see this text!"
70+
71+
fi
72+
73+
# this conditional tests that the given variable
74+
# is a file and actually exists.
75+
if [ -e $VAR ]; then # the -e stands for EXISTS..
76+
77+
echo "You should NOT see this text!"
78+
79+
else
80+
81+
echo "You should see this text!"
82+
83+
fi
84+
85+
# this conditional tests that the given variable
86+
# is a directory and actually exists.
87+
if [ -d $VAR ]; then # the -e stands for EXISTS..
88+
89+
echo "You should NOT see this text!"
90+
91+
else
92+
93+
echo "You should see this text!"
94+
95+
fi
96+
97+
98+
# -------------------------------------
99+
100+
101+
# this conditional tests that the given variable
102+
# is not an empty string.
103+
if [ ! -z $VAR ]; then # the -z stands for zero, as in zero content.
104+
105+
echo "You should see this text!"
106+
107+
else
108+
109+
echo "You should NOT see this text!"
110+
111+
fi

0 commit comments

Comments
 (0)