Course info

Rating

(502)

Level

Intermediate

Updated

Oct 16, 2013

Duration

4h 33m

Description

Bash shell scripts allow you to automate just about any task on a UNIX system. They combine the power of all the UNIX userland utilities with a powerful scripting language. Whether you are a system administrator, a developer or a power user, shell scripts will make you more productive.

About the author

After years of working in software development, Reindert-Jan Ekker has
decided to pursue another passion of his: education. He currently
works as a college professor of Computer Science in the Netherlands,
teaching many subjects like web development, algorithms and data
structures and Scrum.

Section Introduction Transcripts

IntroductionHello. My name is Reindert-Jan Ekker, and welcome to this course called Shell Scripting with Bash. In this first module, I'll give you an overview of the course. First of all, I'll address the question why you would actually need shell scripting. I'll also tell you what you can expect from watching this course, and I'll tell you what you need to be able to follow the course.

A First Look At Shell ScriptsHi. I am Reindert-Jan Ekker, and welcome to the second module of this course called A First Look at Shell Scripts. The aim of this module is to give you an impression of what a shell script is and what you need to write a shell script. Now, we will create two very simple scripts, and while doing this we will learn the following: First, of course, what a shell script actually is and how to create one; how to choose a name for your script that doesn't conflict with other names; how to set the right permissions; and, of course, very importantly how to run your scripts.

VariablesHi. This is Reindert-Jan Ekker, and welcome to this module which will teach you how to use variables in a Bash script. A script that doesn't use variables is probably kind of useless. You need variables to contain the state of your program and to remember things, and without them there are a lot of things you can't do like use input, change stuff, do logic and arithmetic, and many others. After watching this module, you'll know how to create a variable and how to assign it a value and how to use that value. I'll also show you how to pick valid names for your variables, and I'll show you some important things you need to remember about using variables.

If, Then, ElseHi. This is Reindert-Jan Ekker, and welcome to this module where we'll discuss how to make decisions in your script using if, then, else. Now, any really useful computer program needs to be able to react to its input and behave differently depending on what kind of data it receives, and shell scripts are no different. In this module, I'll show you the basics of decision making in shell script. We'll start with looking at the if, then, else statement, which is the basic mechanism to do this. Then I'll discuss return codes, which are the values that the if, then, else statement uses to make its decisions. And finally, we'll see some special Bash syntax for testing all kinds of values called a conditional expression.

Input and OutputHello. I'm Reindert-Jan Ekker, and welcome to this module where I'll discuss how to do input and output with the Bash scripts. Now, I'm guessing I don't have to explain why it's important to know about input and output. After watching this module, you'll know some more about how to generate nice output for your scripts including more about the echo command and a new, more advanced command called printf, and we'll see some more about reading input with the read commands. We'll also look into the three standard streams that your script uses for input and output and how to manipulate these streams using redirection.

Control FlowHi. This is Reindert-Jan Ekker, and welcome to this module called Control Flow. At the start of this course I said that Bash executes the lines in your script in order from top to bottom, but you already know by now that this is not actually correct. With an if statement, for example, only part of the lines in the script will be executed. We call the order in which the lines of code are executed control flow, and in this module I will show you several ways to manipulate this flow. To start with, we'll see several kinds of loops allowing us to repeat a block of code. First we'll look at while and until, which test for a condition every repetition. We'll also see the for loop, which repeats for a fixed number of times. Then we'll see how to prematurely end the loop with break and how to skip a cycle with continue. Another structure you'll be learning about is called case, which lets you choose among several alternatives. Then we'll have a short look at how to group multiple commands into a block called a compound command, and we'll see another use for these two operators we know as an and or.

Variables 2Hi. I am Reindert-Jan Ekker, and welcome to this module which will show you some advanced features of Bash variables. Of course we've already done a module about variables, but in this module I want to expand on that and show you some extra features you can use with variables. First of all, variables in Bash have attributes, which you can set with the declare clause. For example, you can tell Bash that a variable should only hold integers. When you do that, you can use special expressions called arithmetic expressions to do calculations. You can also mark a variable as being read-only, which means you won't be able to change its value after its set. And I want to tell you some more about what it means to export a variable and how you can do this. Finally, I'll tell you how to store multiple values in a single variable with a special kind of variable called an array.

Handling Script ParametersHi. Welcome to this module which will teach you how to correctly handle the arguments given to your script. In the real world, shell scripts will not simply take one or two arguments. They tend to have any number of options, and some of these are required and others aren't, and there may even be options that take extra arguments for themselves. Now, after watching this module, you'll know several ways to handle these scenarios. During this module, we'll see several special variables defined by Bash. These are the positional parameters with numbers 1, 2, etc. , which we already know about, and the star and at variables, which hold all arguments given to your scripts; the hash, which gives you the number of arguments; and $0, which looks like, but actually isn't a positional parameter. To handle the parameters given to your script, there are two commands we will be looking at, namely shift and getopts.

Shell FunctionsHi. This is Reindert-Jan Ekker, and in this module I'm going to talk to you about how to use functions in a shell script. Now, like any decent programming language, Bash will let you define a function in your script, and after watching this module you'll know how to declare a function and how to call it and also how you can have a function return some useful data that you can use after that in the rest of your script. You can also export functions. Now, during this module I'm going to show you several demos, and we'll also come across some features of Bash that I didn't talk about yet.

Fun with StringsHi. This is Reindert-Jan Ekker, and in this module we'll see some techniques for manipulating strings with Bash. The first thing we'll see is some new syntax called parameter expansion, and that will let you do various things. First we'll look at removing a pattern from a string, then we'll see how to search for a pattern and replace it, and finally, we'll see how to give your variable a default value when it's not set. The second thing we'll look into is how to do pattern matching inside a conditional expression. And then finally there's something more or less unrelated called the end of options.

Many Ways to Run Your ScriptHi. I'm Reindert-Jan Ekker, and in the last module of this course I'll show you many ways to run your scripts. I'll start by showing you several ways to run code from a file. Then I'll talk a bit about putting your code in the background and using the nohup command. Then we'll talk about the exit command and how you can use it to provide global redirection for your whole script. We'll also see how to make sure that your script runs at another time than right now. And finally, I want to discuss two commands, set and shopt, with which you can change the behavior of Bash when it runs your code.