0
1
00:00:03,150 --> 00:00:07,620
In this lesson, we're going to look at what shell scripts are, why you should learn them, and how they
1

2
00:00:07,620 --> 00:00:09,810
can be helpful to you.
2

3
00:00:09,840 --> 00:00:16,200
First off, a shell script is simply a file that contains a series of Linux commands and shell statements.
3

4
00:00:16,750 --> 00:00:22,230
The shell script itself, the file, is made up of ASCII text also called plain text.
4

5
00:00:22,230 --> 00:00:26,190
This means that you need to use a text editor to create shell scripts.
5

6
00:00:26,190 --> 00:00:32,490
Some good command line text editors include vim, emacs, and nano. If you want to create shell scripts in a
6

7
00:00:32,490 --> 00:00:35,480
desktop environment using a graphical user interface,
7

8
00:00:35,670 --> 00:00:42,050
you can use text editors like Atom, Sublime Text, Notepad++, and Text Wrangler.
8

9
00:00:42,090 --> 00:00:44,610
I'm being very careful with my words here.
9

10
00:00:44,700 --> 00:00:51,780
I'm saying text editor for a reason. Do not use a word processor such as Microsoft Word, Libreoffice
10

11
00:00:51,780 --> 00:00:54,960
Writer or Apple's Pages to create shell scripts.
11

12
00:00:54,960 --> 00:01:00,400
Many word processors add additional formatting and make changes to the text that you're typing in.
12

13
00:01:00,420 --> 00:01:07,080
One common thing word processors do is they turn normal, straight quotation marks into curly quotation
13

14
00:01:07,080 --> 00:01:07,810
marks.
14

15
00:01:08,100 --> 00:01:13,830
Those straight quotation marks in shell scripts are actually part of the syntax, so using fancy or curly
15

16
00:01:13,830 --> 00:01:20,370
quotation marks will cause your shell scripts to either report an error or produce some unexpected behaviors.
16

17
00:01:20,400 --> 00:01:26,820
Also, these plain text files do not include any other types of special formatting such as bold text, italics,
17

18
00:01:26,820 --> 00:01:27,820
and so on.
18

19
00:01:28,140 --> 00:01:33,750
When a shell script is executed, it in turn executes the commands listed in the script.
19

20
00:01:33,750 --> 00:01:39,420
It starts at the top and executes the commands on each line one line at a time until the end of the
20

21
00:01:39,420 --> 00:01:40,120
file.
21

22
00:01:40,440 --> 00:01:46,080
Executing a shell script will produce the exact same result as if you had typed in each line by hand
22

23
00:01:46,290 --> 00:01:48,090
directly into the terminal.
23

24
00:01:48,150 --> 00:01:52,550
So, shell scripts are plain text files that contain a series of commands.
24

25
00:01:52,620 --> 00:01:55,920
But why would you want to save a series of commands into a script?
25

26
00:01:56,070 --> 00:01:59,550
For starters, it can save you a lot of time in a work.
26

27
00:01:59,690 --> 00:02:04,410
Any time you find yourself entering multiple shell commands in the terminal in order to accomplish a
27

28
00:02:04,410 --> 00:02:10,090
task and you'll need to do that task again in the future, it's time to write a shell script.
28

29
00:02:10,380 --> 00:02:15,030
Once you figure out the set of commands that needs to be executed in order to get the work done, you
29

30
00:02:15,030 --> 00:02:20,520
can put those commands in a shell script and simply execute the script itself the next time you need
30

31
00:02:20,520 --> 00:02:22,020
to do that task.
31

32
00:02:22,020 --> 00:02:26,820
If it's a task that has to be done on a regular basis that's even better because you can write a shell
32

33
00:02:26,820 --> 00:02:32,220
script and then schedule that script to run at a certain time and then you'll never have to do it yourself
33

34
00:02:32,220 --> 00:02:33,130
again.
34

35
00:02:33,180 --> 00:02:39,630
So, shell scripts save you time and effort by allowing you to eliminate repetitive tasks through automation.
35

36
00:02:39,870 --> 00:02:45,450
That means instead of typing the same commands over and over again you can spend that time doing more
36

37
00:02:45,450 --> 00:02:48,150
valuable and higher level tasks.
37

38
00:02:48,150 --> 00:02:52,950
Another good time to write a shell script is for a task that you have to do more than once, but it's
38

39
00:02:52,950 --> 00:02:54,760
something that you rarely do.
39

40
00:02:54,960 --> 00:02:59,350
I'll give you example of a task I had to do at one of my previous jobs.
40

41
00:02:59,400 --> 00:03:04,200
Each time a new version of this one particular application came out I would have to put a copy of the
41

42
00:03:04,200 --> 00:03:09,960
production database into our test environment so that the new version of the application could be tested
42

43
00:03:10,110 --> 00:03:13,470
against actual production data on the production side.
43

44
00:03:13,470 --> 00:03:19,860
This involves suspending rights to the database, creating a clone of the underlying SAN disks, and resuming
44

45
00:03:19,860 --> 00:03:25,290
the database. To my initial point, because I rarely did this I would end up forgetting the command to
45

46
00:03:25,290 --> 00:03:30,540
initiate the clone and all the options that were required to go with it. Each time I did this I had to
46

47
00:03:30,540 --> 00:03:36,030
spend some time rereading the man page and more or less reteaching myself how to do this process.
47

48
00:03:36,030 --> 00:03:39,370
That alone was reason enough for me to create a shell script.
48

49
00:03:39,390 --> 00:03:44,250
If we go beyond my original point of "it's something that I rarely did," there was another reason to write
49

50
00:03:44,250 --> 00:03:45,140
a shell script.
50

51
00:03:45,360 --> 00:03:51,000
You see I wanted to make sure that the production database was paused for as little time as possible.
51

52
00:03:51,000 --> 00:03:57,180
I wrote the shell script so as soon as the clone was successfully kicked off, it would resume the database.
52

53
00:03:57,180 --> 00:04:01,980
That way I didn't have to manually rechecked the clone status and then manually type in the command
53

54
00:04:01,980 --> 00:04:03,420
to resume the database.
54

55
00:04:03,420 --> 00:04:06,850
The script did it and it did it faster than a human could do it.
55

56
00:04:06,990 --> 00:04:13,250
A third take away from this one example is that since this was the company's most valuable database
56

57
00:04:13,560 --> 00:04:17,300
I really didn't want to take any chances and make a mistake.
57

58
00:04:17,430 --> 00:04:22,290
Once I tested my script in a non production environment, I could let it do the hard work of picking out
58

59
00:04:22,290 --> 00:04:23,590
all the correct disks,
59

60
00:04:23,670 --> 00:04:26,640
typing all the right commands with the right options, and so on.
60

61
00:04:26,820 --> 00:04:30,270
Using a good script can reduce the chance of errors.
61

62
00:04:30,360 --> 00:04:34,880
Many times I would have to do this at midnight when I was tired and not as alert as usual.
62

63
00:04:35,010 --> 00:04:37,590
When you're tired, the chances of making mistakes go up.
63

64
00:04:37,590 --> 00:04:42,650
So, to have some extra help in the form of a shell script was a very good thing for me.
64

65
00:04:42,720 --> 00:04:47,880
If you want to delegate a task to less experienced or knowledgeable staff then create a shell script
65

66
00:04:47,880 --> 00:04:48,510
for it.
66

67
00:04:48,510 --> 00:04:54,270
If you have some specific knowledge of how a piece of work needs to be performed, you can put that knowledge
67

68
00:04:54,300 --> 00:04:55,410
in a shell script.
68

69
00:04:55,500 --> 00:04:58,320
The shell script then becomes a black box to others,
69

70
00:04:58,410 --> 00:05:01,340
but they can still use it to get the same result.
70

71
00:05:01,350 --> 00:05:08,020
Take, for example, an employee separation process. Let's say someone from human resources submits a help
71

72
00:05:08,020 --> 00:05:13,930
desk ticket to notify the information technology department that a given employee has left the company.
72

73
00:05:14,020 --> 00:05:19,570
The helpdesk then transfers that ticket to you so you can lock the Linux account on all the Linux systems.
73

74
00:05:19,990 --> 00:05:22,420
If you created a shell script that perform the work,
74

75
00:05:22,540 --> 00:05:25,480
you could hand this task over to the help desk.
75

76
00:05:25,510 --> 00:05:28,760
They don't have to know all the details of how the shell script actually works,
76

77
00:05:28,780 --> 00:05:31,930
they just need instructions on how and when to use it.
77

78
00:05:31,930 --> 00:05:38,440
That frees you up from that particular task. If you need information from more than one command or source,
78

79
00:05:38,440 --> 00:05:43,770
that is another great time to create a shell script. If you ever find yourself making notes,
79

80
00:05:43,780 --> 00:05:46,200
that's a good sign there's an easier way to do it.
80

81
00:05:46,210 --> 00:05:48,860
For example, let's say you manage a web server
81

82
00:05:48,880 --> 00:05:55,600
and for each IP address that generated a web server error, you wanted to know their host name as well as
82

83
00:05:55,600 --> 00:05:58,860
see all the web pages that they visited.
83

84
00:05:59,080 --> 00:06:04,010
If you're doing this by hand you would have to look through the error log and note each IP address.
84

85
00:06:04,150 --> 00:06:09,760
Perhaps you would copy and paste these IP addresses and put them in a scrap file, for example, and then
85

86
00:06:09,760 --> 00:06:13,470
what you would do is look up each IP address to determine their hostname.
86

87
00:06:13,480 --> 00:06:18,060
Finally,  you would search for those IP addresses in the web server access logs.
87

88
00:06:18,070 --> 00:06:23,830
Now you could write a shell script that does this entire process for you and displays it in an easy
88

89
00:06:23,830 --> 00:06:26,520
to consume and easy to read format.
89

90
00:06:26,830 --> 00:06:30,020
OK, so I've given you a couple of examples of shell scripts.
90

91
00:06:30,070 --> 00:06:35,410
When you think about it, shell scripts are really just computer programs written in the shell language.
91

92
00:06:35,410 --> 00:06:41,170
So why would you choose to write programs in shell instead of writing programs in a compiled language
92

93
00:06:41,170 --> 00:06:43,510
such as C or C++?
93

94
00:06:43,540 --> 00:06:46,550
The main reason is speed of development.
94

95
00:06:46,570 --> 00:06:51,220
For example, you can write a shell script that will perform a simple backup by first archiving a set
95

96
00:06:51,220 --> 00:06:55,560
of files and then copying that archive over the network to another server.
96

97
00:06:55,750 --> 00:07:00,310
You can do that in just a few lines of shell  code and be done fairly quickly.
97

98
00:07:00,550 --> 00:07:06,140
However accomplishing that same task in C would take a lot of time and effort.
98

99
00:07:06,340 --> 00:07:10,570
When you're writing shell scripts you don't have to worry about low level programming objects such as
99

100
00:07:10,570 --> 00:07:11,960
bytes, pointers,
100

101
00:07:12,060 --> 00:07:14,160
declaring data types, and so on.
101

102
00:07:14,320 --> 00:07:21,070
The shell is a high level language that allows you to express complex operations simply and clearly.
102

103
00:07:21,550 --> 00:07:26,430
Related to the speed of development is the ease and speed of learning how to shell script.
103

104
00:07:26,530 --> 00:07:31,430
If you use the command line already, then the leap to creating shell scripts is really a small one.
104

105
00:07:31,570 --> 00:07:37,180
You just need to learn a few shell specific operators and rules and you're ready to start.
105

106
00:07:37,180 --> 00:07:41,590
Of course, if you want to learn advanced techniques and write really complicated shell scripts, it's going
106

107
00:07:41,590 --> 00:07:47,370
to take more time and effort, but the initial learning curve is relatively low for simple scripts.
107

108
00:07:47,380 --> 00:07:51,470
In all fairness, there is a trade off for the ease and speed of development.
108

109
00:07:51,550 --> 00:07:56,920
That trade off comes at the cost of performance and efficiency. That shell script that you wrote in 30
109

110
00:07:56,920 --> 00:08:00,440
minutes will not execute as fast as the equivalent program
110

111
00:08:00,550 --> 00:08:07,050
that might take days to write in C++, but the trade off is often well worth it, especially when the shell
111

112
00:08:07,050 --> 00:08:10,290
script will run fast enough for the intended purpose.
112

113
00:08:10,330 --> 00:08:16,150
You can create shell scripts for almost anything, but the real sweet spot is typically for System Administration
113

114
00:08:16,150 --> 00:08:21,970
and reporting tests where getting the job done is more important than getting the job done a few microseconds
114

115
00:08:21,970 --> 00:08:22,930
quicker.
115

116
00:08:22,930 --> 00:08:28,300
I want you to keep in mind that any task that you can do on the command line can be automated by a shell
116

117
00:08:28,300 --> 00:08:28,990
script.
117

118
00:08:28,990 --> 00:08:29,990
I want to see that again.
118

119
00:08:30,010 --> 00:08:33,960
Any task you can do in the command line can be automated by a script.
119

120
00:08:34,090 --> 00:08:39,020
Of course, some scripts are going to be harder than others,  but you can do it. In this course we'll cover 
120

121
00:08:39,100 --> 00:08:45,310
the simple examples all the way to the very complex situations where many people might think it's practically 
121

122
00:08:45,310 --> 00:08:47,730
impossible to automate such a task.
122

123
00:08:47,950 --> 00:08:53,920
To quickly recap, a shell script is a plain text file that contains a series of commands. When a shell
123

124
00:08:53,920 --> 00:08:57,980
script is executed, it in turn executes the commands listed in the script
124

125
00:08:58,090 --> 00:09:04,090
line by line starting at the top of the script just as if you had typed the commands in yourself.
125

126
00:09:04,390 --> 00:09:07,520
Shell scripts can save you a lot of time in work.
126

127
00:09:07,540 --> 00:09:10,790
You can fully automate tedious or repetitive tasks.
127

128
00:09:11,110 --> 00:09:15,300
You can hand off work to others by providing them a script to perform an action.
128

129
00:09:15,310 --> 00:09:21,230
Scripts can also act as a form of documentation for a process or procedure that you perform in frequently.
129

130
00:09:21,340 --> 00:09:24,620
In general, shell scripts are fairly quick and easy to write.
130

131
00:09:24,730 --> 00:09:29,860
Instead of spending your time worrying about low level programming tasks, you can focus on accomplishing
131

132
00:09:29,860 --> 00:09:30,960
the goal at hand.
132

133
00:09:31,270 --> 00:09:36,050
And finally anything that you can do on the command line can be automated with the shell script.
