WEBVTT

1
00:00:00.210 --> 00:00:03.360
In this lesson, we will learn about Bash.

2
00:00:03.360 --> 00:00:06.660
Bash, which stands for Borne Again Shell,

3
00:00:06.660 --> 00:00:09.150
is a Unix shell and command language

4
00:00:09.150 --> 00:00:11.100
used to write scripts.

5
00:00:11.100 --> 00:00:15.180
Bash scripting concepts include both data structures

6
00:00:15.180 --> 00:00:17.130
and control structures.

7
00:00:17.130 --> 00:00:19.320
In Bash, data structures,

8
00:00:19.320 --> 00:00:22.620
such as variables, arrays, and strings,

9
00:00:22.620 --> 00:00:24.600
are essential for organizing

10
00:00:24.600 --> 00:00:27.570
and storing data within the scripts.

11
00:00:27.570 --> 00:00:29.940
By using these data structures,

12
00:00:29.940 --> 00:00:33.090
Bash scripts can handle complex tasks

13
00:00:33.090 --> 00:00:36.720
by processing and organizing multiple elements,

14
00:00:36.720 --> 00:00:39.000
such as a series of commands,

15
00:00:39.000 --> 00:00:41.760
configurations, or input data,

16
00:00:41.760 --> 00:00:43.260
in a structured way.

17
00:00:43.260 --> 00:00:45.780
Next, control structures in Bash,

18
00:00:45.780 --> 00:00:48.390
such as loops like for and while

19
00:00:48.390 --> 00:00:51.480
and conditional statements like if and else,

20
00:00:51.480 --> 00:00:54.600
dictate the flow of script execution.

21
00:00:54.600 --> 00:00:55.650
In this manner,

22
00:00:55.650 --> 00:00:58.230
control structures enable those scripts

23
00:00:58.230 --> 00:01:00.420
to perform repeated actions

24
00:01:00.420 --> 00:01:04.140
and make decisions based on specific conditions.

25
00:01:04.140 --> 00:01:07.230
Let's learn more about Bash data structures

26
00:01:07.230 --> 00:01:09.330
and control structures.

27
00:01:09.330 --> 00:01:12.420
First, we have Bash data structures.

28
00:01:12.420 --> 00:01:16.560
In Bash, variables are a fundamental data structure

29
00:01:16.560 --> 00:01:19.140
used to store single values.

30
00:01:19.140 --> 00:01:21.090
In variable declaration,

31
00:01:21.090 --> 00:01:24.210
Bash does not require the use of a keyword,

32
00:01:24.210 --> 00:01:26.850
like var or let.

33
00:01:26.850 --> 00:01:29.040
Some other scripting languages

34
00:01:29.040 --> 00:01:31.470
do require a keyword like that

35
00:01:31.470 --> 00:01:33.480
when declaring a variable.

36
00:01:33.480 --> 00:01:36.300
In Bash, you simply assign a value

37
00:01:36.300 --> 00:01:38.850
using the = operator.

38
00:01:38.850 --> 00:01:43.620
However, in Bash, no spaces should surround the equal sign.

39
00:01:43.620 --> 00:01:46.980
For example, in the code that appears on the screen,

40
00:01:46.980 --> 00:01:50.250
we can see that the username is the variable,

41
00:01:50.250 --> 00:01:52.530
and it holds the value admin.

42
00:01:52.530 --> 00:01:56.100
Bash variables, like the username variable in this case,

43
00:01:56.100 --> 00:01:58.080
can hold strings or numbers,

44
00:01:58.080 --> 00:02:00.180
but everything in Bash is treated

45
00:02:00.180 --> 00:02:03.540
as a string unless otherwise specified.

46
00:02:03.540 --> 00:02:06.030
A string is a sequence of characters,

47
00:02:06.030 --> 00:02:08.790
such as letters, numbers, and symbols,

48
00:02:08.790 --> 00:02:10.860
used to represent text.

49
00:02:10.860 --> 00:02:13.950
Accessing the value of a variable in Bash

50
00:02:13.950 --> 00:02:17.250
requires a $ before the variable name

51
00:02:17.250 --> 00:02:19.170
as seen in the echo command,

52
00:02:19.170 --> 00:02:22.650
which is used to print out information on the screen.

53
00:02:22.650 --> 00:02:26.640
Next, let's look at a script snippet that uses an array

54
00:02:26.640 --> 00:02:28.950
and prints out an array element.

55
00:02:28.950 --> 00:02:32.670
Arrays in Bash allow you to store multiple values

56
00:02:32.670 --> 00:02:34.200
in a single variable,

57
00:02:34.200 --> 00:02:37.830
and in an array, Bash uses zero-based indexing

58
00:02:37.830 --> 00:02:40.050
to access array elements.

59
00:02:40.050 --> 00:02:42.840
This means that the first element in an array

60
00:02:42.840 --> 00:02:45.090
is in the 0 position,

61
00:02:45.090 --> 00:02:48.600
and the second element is in the 1 position.

62
00:02:48.600 --> 00:02:50.910
Furthermore, arrays are created

63
00:02:50.910 --> 00:02:52.920
by assigning multiple values

64
00:02:52.920 --> 00:02:56.250
within quotations separated by spaces,

65
00:02:56.250 --> 00:02:58.800
as you can see in the script example.

66
00:02:58.800 --> 00:03:00.120
In this example,

67
00:03:00.120 --> 00:03:03.630
the servers array contains three values.

68
00:03:03.630 --> 00:03:05.310
In the second part of the script,

69
00:03:05.310 --> 00:03:08.550
echo is used to print out the First server

70
00:03:08.550 --> 00:03:10.740
and then a colon to the screen.

71
00:03:10.740 --> 00:03:15.740
Next, the ${servers[0]} retrieve the first value

72
00:03:16.950 --> 00:03:18.840
or element of the array,

73
00:03:18.840 --> 00:03:21.150
which is in the 0 position.

74
00:03:21.150 --> 00:03:24.360
In this case, that is server1.

75
00:03:24.360 --> 00:03:26.430
Overall, arrays are useful

76
00:03:26.430 --> 00:03:29.010
when you need to store a list of items,

77
00:03:29.010 --> 00:03:31.170
such as a list of server names,

78
00:03:31.170 --> 00:03:34.410
and loop through them to perform operations.

79
00:03:34.410 --> 00:03:38.610
Finally, Bash can be used to store and access strings.

80
00:03:38.610 --> 00:03:41.460
Strings are simple data types in Bash,

81
00:03:41.460 --> 00:03:42.750
but they are powerful

82
00:03:42.750 --> 00:03:45.390
when combined with other data structures.

83
00:03:45.390 --> 00:03:49.380
For examples, strings can be concatenated using variables

84
00:03:49.380 --> 00:03:50.760
or direct text,

85
00:03:50.760 --> 00:03:52.320
and special operators,

86
00:03:52.320 --> 00:03:55.140
like the hash sign or the percent sign,

87
00:03:55.140 --> 00:03:57.570
are used for string manipulation.

88
00:03:57.570 --> 00:04:00.600
For example, the following Bash script snippet

89
00:04:00.600 --> 00:04:03.660
allows you to find the length of a string.

90
00:04:03.660 --> 00:04:07.320
This script snippet assigns the value Hello, world!

91
00:04:07.320 --> 00:04:09.000
to the variable greeting.

92
00:04:09.000 --> 00:04:11.250
Then it uses the echo command

93
00:04:11.250 --> 00:04:14.160
to print the length of the string greeting.

94
00:04:14.160 --> 00:04:18.300
In Bash, the ${#}

95
00:04:18.300 --> 00:04:20.880
is used to identify the string

96
00:04:20.880 --> 00:04:23.070
whose length should be counted.

97
00:04:23.070 --> 00:04:25.170
So this command would print out

98
00:04:25.170 --> 00:04:28.230
The length of the greeting is 13.

99
00:04:28.230 --> 00:04:31.500
Second, we have Bash control structures.

100
00:04:31.500 --> 00:04:35.010
Control structures dictate the flow of execution

101
00:04:35.010 --> 00:04:38.970
by enabling decision making and repeated actions.

102
00:04:38.970 --> 00:04:42.960
Conditional statements, such as if, elif, and else,

103
00:04:42.960 --> 00:04:45.240
are used to evaluate conditions

104
00:04:45.240 --> 00:04:47.640
and execute different blocks of code,

105
00:04:47.640 --> 00:04:50.730
depending upon whether the conditions are met.

106
00:04:50.730 --> 00:04:52.260
In this code snippet,

107
00:04:52.260 --> 00:04:56.910
Bash uses the if keyword to open a conditional block

108
00:04:56.910 --> 00:05:00.450
and the fi or fi to close the block.

109
00:05:00.450 --> 00:05:02.940
As you can see in this example,

110
00:05:02.940 --> 00:05:05.370
the if conditional statement checks

111
00:05:05.370 --> 00:05:09.570
whether the variable user is equal to admin.

112
00:05:09.570 --> 00:05:13.920
The evaluated condition is enclosed in square brackets.

113
00:05:13.920 --> 00:05:15.810
If the condition is true,

114
00:05:15.810 --> 00:05:18.480
it executes the code after then

115
00:05:18.480 --> 00:05:21.480
to output to the screen "Welcome, admin!"

116
00:05:21.480 --> 00:05:24.030
Otherwise, it runs the else block,

117
00:05:24.030 --> 00:05:26.850
printing out to the screen "Access denied."

118
00:05:26.850 --> 00:05:29.280
The if statement ends with a fi,

119
00:05:29.280 --> 00:05:32.280
signaling the end of the if block.

120
00:05:32.280 --> 00:05:36.300
Next, loops are another important control structure in Bash,

121
00:05:36.300 --> 00:05:39.660
allowing you to execute commands repeatedly.

122
00:05:39.660 --> 00:05:42.480
The for loop is often used to iterate

123
00:05:42.480 --> 00:05:44.250
over lists or arrays,

124
00:05:44.250 --> 00:05:46.230
making it a powerful tool

125
00:05:46.230 --> 00:05:48.960
for automating repetitive tasks.

126
00:05:48.960 --> 00:05:51.690
Take a look at the code example on the screen.

127
00:05:51.690 --> 00:05:53.070
In this example,

128
00:05:53.070 --> 00:05:56.400
the for loop iterates over the servers array,

129
00:05:56.400 --> 00:05:58.740
and for each element in the array,

130
00:05:58.740 --> 00:06:03.030
denoted by the square brackets with the @ sign within them,

131
00:06:03.030 --> 00:06:05.280
it executes the echo command,

132
00:06:05.280 --> 00:06:08.790
printing out to the screen "Connecting to server1",

133
00:06:08.790 --> 00:06:11.700
"Connecting to server2", et cetera.

134
00:06:11.700 --> 00:06:12.780
In this snippet,

135
00:06:12.780 --> 00:06:16.260
the do and done keywords mark the beginning

136
00:06:16.260 --> 00:06:18.690
and end of the loop block.

137
00:06:18.690 --> 00:06:20.160
Next, in Bash,

138
00:06:20.160 --> 00:06:24.330
a while loop continues to execute a block of code

139
00:06:24.330 --> 00:06:28.290
as long as a specified condition remains true.

140
00:06:28.290 --> 00:06:30.150
Similar to the if statement,

141
00:06:30.150 --> 00:06:33.030
the condition is enclosed in square brackets,

142
00:06:33.030 --> 00:06:36.270
and the block is marked with do and done.

143
00:06:36.270 --> 00:06:39.510
Take a look at the code snippet on the screen.

144
00:06:39.510 --> 00:06:41.100
In this code snippet,

145
00:06:41.100 --> 00:06:42.660
the while loop runs

146
00:06:42.660 --> 00:06:47.660
as long as the variable count is less than or equal to 5.

147
00:06:47.760 --> 00:06:51.930
Each iteration that runs prints out the value of count

148
00:06:51.930 --> 00:06:54.240
and increments the count variable

149
00:06:54.240 --> 00:06:58.200
by one using the ((count++)) syntax.

150
00:06:58.200 --> 00:07:01.980
Finally, case statements in Bash offer an alternative

151
00:07:01.980 --> 00:07:03.120
to if statements

152
00:07:03.120 --> 00:07:06.300
when there are multiple conditions to evaluate.

153
00:07:06.300 --> 00:07:08.430
This is particularly useful

154
00:07:08.430 --> 00:07:10.470
when you need to match a variable

155
00:07:10.470 --> 00:07:13.230
against several possible values.

156
00:07:13.230 --> 00:07:15.840
Let's review the following script snippet

157
00:07:15.840 --> 00:07:17.550
that appears on the screen.

158
00:07:17.550 --> 00:07:18.870
In this example,

159
00:07:18.870 --> 00:07:23.160
the script evaluates the value of the variable Option

160
00:07:23.160 --> 00:07:26.640
and executes a corresponding block of code.

161
00:07:26.640 --> 00:07:30.150
The double semicolon at the end of each case block

162
00:07:30.150 --> 00:07:32.490
is used to separate each case,

163
00:07:32.490 --> 00:07:36.270
and the esac, which is case written backward,

164
00:07:36.270 --> 00:07:39.420
marks the end of the entire case block.

165
00:07:39.420 --> 00:07:42.120
So by combining control structures,

166
00:07:42.120 --> 00:07:45.210
like if, for, while, and case,

167
00:07:45.210 --> 00:07:48.690
Bash scripts can handle complex decision-making

168
00:07:48.690 --> 00:07:50.400
and task automation,

169
00:07:50.400 --> 00:07:52.380
making them highly flexible

170
00:07:52.380 --> 00:07:55.980
for system management and security tasks.

171
00:07:55.980 --> 00:07:59.670
So remember, Bash is a Unix shell

172
00:07:59.670 --> 00:08:01.860
and command language designed

173
00:08:01.860 --> 00:08:04.050
to automate system tasks,

174
00:08:04.050 --> 00:08:08.160
including system management and security operations.

175
00:08:08.160 --> 00:08:10.080
It uses data structures,

176
00:08:10.080 --> 00:08:12.990
like variables, arrays and strings,

177
00:08:12.990 --> 00:08:17.250
to store and organize data efficiently within scripts,

178
00:08:17.250 --> 00:08:19.860
allowing for complex operations

179
00:08:19.860 --> 00:08:23.490
by managing and processing multiple elements.

180
00:08:23.490 --> 00:08:25.830
Control structures, such as loops

181
00:08:25.830 --> 00:08:27.390
and conditional statements,

182
00:08:27.390 --> 00:08:29.760
guide the flow of execution,

183
00:08:29.760 --> 00:08:33.390
enabling scripts to perform repeated actions

184
00:08:33.390 --> 00:08:37.230
or make decisions based on specific conditions.

185
00:08:37.230 --> 00:08:41.580
These structures are key to automating repetitive tasks,

186
00:08:41.580 --> 00:08:45.300
improving efficiency in system operations.

187
00:08:45.300 --> 00:08:48.960
Overall, Bash scripting provides flexibility

188
00:08:48.960 --> 00:08:52.863
for managing Unix-based systems through automation.

