The course is part of this learning path
This course covers three rather distinct topics, though properly understanding each is critically important for managing Linux systems:
- Managing shell session environments
- Working with Linux scripts
- Working with Linux databases (MySQL)
- Migrating a MySQL database to Amazon's Relational Database Service
Explore the entire Linux certification series.
If you have thoughts or suggestions for this course, please contact Cloud Academy at firstname.lastname@example.org.
In this video and the next, we're going to learn about Linux scripting. Scripting lies somewhere in between system administration and programming on the one hand, like programming, Bash scripts allow for loops, control structures and dynamic data handling. But on the other hand the syntax and execution is very closely integrated with the command line. In fact, any command that's possible to run directly on the command line can be used as part of a script. Because scripts share the qualities of both of these worlds, they're perfectly suited for their core task of automating administration tasks.
Introduction to Linux scripting
Let's begin at the beginning. A script is a plain text file that will often use a .sh extension. The first line of the script, known for some reason as the shebang line, identifies the interpreter you plan to use. In our case, we'll type #!/bin/bash which tells Linux that we want this script to use the rules of the Bash shell. Besides that first line, any line that begins with a hash is ignored by Bash and serves only as a comment. It is a very, very good practice to clearly comment every step of your scripts so that anyone reading it later will have some clue of what it's supposed to do. This will not only help the poor fellow who comes after you but could serve you extremely well if you're troubleshooting your own script months after you wrote it trying to figure out what stopped working and why. So I'll take my own advice and add a comment to this first script.
For this simple example, we'll echo a message exactly the way we've seen it done on the command line. On the next line of our script, I'll type date which is a Linux command that prints the system date to the screen. "Exit 0" will end the script execution. Now we'll save the script file and exit nano so we can actually run the script.
To run a script you type a dot and a forward slash and the name of the file. Whoops something went wrong. I can't tell you many times I made this mistake and just sat staring stupidly at the screen and wondering what I could possibly have done to Linux to make it hate me so much. But here's the problem. Remember back in the previous video when we discussed file permissions? We learned that there are three categories of permission: read, write and execute. Well it seems that you can't execute a script unless the script file is executable. This one obviously isn't. Let's change that now using "chmod +x"and the name of the file." Now let's try that one again.
Let's review. You begin each script with the shebang line. All other lines that start with a hash are ignored and used for comments. You need to make a file executable before it can be run as a script and running a script requires that the file name is prefaced by a dot and slash. \
Using user inputs and arithmetic in Linux scripts
We are now going to explore quite a few examples designed to illustrate various key scripting syntax and command tools. I would advise you to pause the video now and then so you can think through each example for yourself. Make sure you understand what each line does and indeed what each character does. Ideally, you'll open up your own terminal and play around with each of these tools for yourself. I would also advise you to type in the text of the scripts yourself so you get used to the syntax and get a feel for the kinds of mistakes that are commonly made.
Okay we're ready to try something a bit more interactive. This script will ask the user to input some text then using the "read" command it will assign whatever the user types to a variable called answer. The script will print the answer and the words, "Okay it's," and then print the system date. Let's exit and run it. Let's see if we can work with numbers. If you have experience programming, you'll probably have noted that we didn't declare our variable in the last example. However, that's only for simple text strings. If you want Bash to understand your input as an integer, you will have to declare it.
This example first declares three variables as integers. It will then ask the user for two numbers and use read to assign them to the number one and number two variables. Using simple arithmetic symbols like the equal sign or a plus sign you can create an equation that will give the sum of our two input variables to the variable total and print it out. Let's run it.
It's review time again. The read command will accept user input and assign it to a variable. You need to declare variables as integers if you want your script to work with them as numbers. And you can use the normal arithmetic symbols to perform math functions in a very simple way. We'll continue with scripts in the next video.
David taught high school for twenty years, worked as a Linux system administrator for five years, and has been writing since he could hold a crayon between his fingers. His childhood bedroom wall has since been repainted.
Having worked directly with all kinds of technology, David derives great pleasure from completing projects that draw on as many tools from his toolkit as possible.
Besides being a Linux system administrator with a strong focus on virtualization and security tools, David writes technical documentation and user guides, and creates technology training videos.
His favorite technology tool is the one that should be just about ready for release tomorrow. Or Thursday.