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