The course is part of this learning path
This sixth of the eleven courses in our Linux certification series introduces material from the LPIC's second (and final) exam required for your LPIC-1 Server Professional certification. 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 email@example.com.
In this video, we'll continue our study of scripts. Let's learn how to test for an environment condition and only then execute a command. This script will ask the user for a directory you'd like to add to the system path, read the value into NewPath, then use -e, which I believe stands for "exists," to test for the existence of the directory the user has entered. If the directory does exist, it will use PATH=$PATH:$NewPath to add NewPath to the PATH, then export it. And finally, echo the new complete path variable. If, on the other hand, the directory does not exist, then this script will output a suitable message. Note, by the way, how calling an existing variable requires that it be prefaced by a dollar sign for Linux to understand that it's a variable name we're referring to, and not a text string or command. Let's run the program and enter a valid directory. Now we'll run it, but this time we'll enter a non-valid directory.
Let's try out an if-else statement. This script will use test to test for the existence of a file called stuff. If it does exist, in other words, if the test result is positive, then the "stuff exists" message will be printed. In every other case, meaning if else is satisfied, "stuff does not exist" will be printed. Note how you need to close the if-else filter with the letters fi, which is if backwards. We'll run the script.
Testing for environment conditions using "test" in Linux scripts
We should look more deeply into test. As you saw, we added the -f argument, which tells test to confirm that the file not only exists, but that it's a regular file. -d will make test confirm that the target is a directory, and -r will confirm that it's a file, and that it's readable. Test can also be used to compare two values. Here we've given the value of hello to the text1 variable and goodbye to the text2 variable. I would imagine that both of us could tell pretty quickly whether the two are equal. Hint: they're not. Let's see if Linux can figure it out. Using an if-else statement, we'll ask test to confirm whether text1 and text2 are not identical. The exclamation mark and equals sign mean not equal. We'll run the script to show that indeed, just as we suspected, hello and goodbye are not identical. We could also use an equal sign on its own to confirm that two values are identical.
Tests run with the -eq argument will confirm whether two integers are equal to each other. -lt tells us that one integer is less than the other, and -gt tells us that one integer is greater than the other.
Let's review. -e, which is an alternative to test, tests if a resource exists. If-else statements provide simple yes/no deicsion-making. Test -f will test for the existence of a file, -d for a directory, and -r for readable. Test with the equal sign between two strings will test to see if they are the same. Exclamation mark and equal sign will test if they are not the same. You can compare integers with a test -eq, confirming that they're equal, -lt, that the first value is less than the second value, and -gt, that the first value is greater than the second value.
Using conditional statements (if-else) in Linux scripts
If-else statements work well when there are only two possible options: either something is true or it's not. If your choices are more complicated and you need to allow for multiple possible answers, you're better off choosing case. Here's how it could work. This script will take a user input and assign it to the variable color. The words case $COLOR in could be understood as "in the case where color equals one of these possibilities." Meaning either red or blue, the script will echo, "Nice! I love that color." As you can see, a user input of orange or brown will generate a "Not bad . . . " response, and black or purple will get the "Yuk!" message. All other input, covered by the asterisk, will produce the "Sorry, I'm not familiar" error message. Note the rather odd syntax needed to make a case structure work, like the double colons and closing parentheses, or the pipe symbol in place of or. And note also how we close a case structure with the word esac, which is case backwards.
Let's try running the script and inputting blue, then brown and finally green, which wasn't one of our listed examples.
Working with loops in Linux scripts
A while loop will continue executing a command as long as, or in other words, while a specified condition is met. In this case, we've given the variable COUNTER the numeric value of 10. And using COUNTER=COUNTER-1, we're going to reduce that value by one each time through the loop. As long as the value of COUNTER is still greater than two, we'll print the value at the end of the "The counter is" sentence. Once the value of COUNTER drops below three, the done line tells the script to stop. Let's try it.
You can also execute commands through each cycle of a loop using until rather than while. Until will cause the commands to repeat until a certain condition is met, rather than while a condition is still true. In this case, we've set counter to 20 to start off. And again, we'll reduce that by one each cycle through the loop. Now, however, we'll continue the loop only until COUNTER is less than 10. Let's see how that works.
For can execute a command against each item in a list. In this example, we'll use the list of files and directories generated by ls as input, assign it to the variable i, replacing any previous value it might have had, and echo the value to the screen.
Finally, you can, for example, feed the for loop with the output of seq. Let's just run seq directly from the command line to get a feel for how it works. Seq 15 will count up one number at a time, in sequence, hence the name seq, until it gets to 15. Seq 5 15 will count up between five and 15. Seq 3 6 21 will count between three and 21, but by increments of six. Now let's see how that might work within a script. Here we'll feed our variable i with the simple seq 15 command, which will count up by ones between one and 15. For each loop cycle, the script will output the current value of i and the message. Let's run it.
These last few examples may have seemed rather simple, and in fact a bit redundant. However, each of them was included to illustrate a distinct and unique tool which can be used to produce very different results.
Now let's review this material. Case can handle multiple possible conditions, while loops continue executing a command as long as a set condition is still true. Until loops will execute only until a condition is met. For loops will execute a command against items on a list. And you can seed for loops with the output of the seq command, which counts in sequence.
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.