Note: Because this is an ongoing series, viewers will not receive a certificate of completion.
Skill Level Intermediate
- [Instructor] When we're working with Bash scripts, it's important to be able to use numbers and comparisons in our work. Does the storage array have more than 30 terabytes free or less? Does the configuration file exist? Has that process been running for less than a minute? Arithmetic and comparison operators help us answer these questions. Let's take a look at arithmetic operators first. Bash supports common arithmetic operators like addition, subtraction, multiplication, and division. And it also supports exponentiation and modulo operators as well.
Let's use arithmetic expansion here at the command line. To do that, I'll write echo and then a $ and two opening parentheses. Let's add 5+4 and follow that up with two closing parentheses. I see the result of nine, alright. Now let's subtract, I'll write echo $ opening parentheses 5-4. Good so far, now let's multiply.
Okay, and division. I'll write 5/4 and, well, what's going on here? That should be one and a quarter. One thing to remember is that math in Bash only works with integers. Bash considers floating point numbers to be strings and the results of math operations, even if the result normally evaluates to a floating point number, is also an integer. So Bash isn't well suited to precise calculations, but the tools we have to work with integers are helpful in a system administration sense.
So you might need to think about numbers a little bit when using these operators in your scripts. Instead of asking whether 1.8 terabytes is available, ask about 1,800 gigabytes instead. Check for 90 seconds instead of a minute and a half, and so on. If you need precision for financial or scientific work, Bash is not the best choice. We can do exponentiation, as well. Raising the first term to the power of the second term. To find 5 to the 4th power, I'll write 5**4 and for modulo, I'll write 5%4.
We can use these operations with variables, as well. I'll set a=5 and b=4 and then I can write echo a+b. We can also increment and decrement variables. To increment the variable a, I'll write echo $((a+=1)) Notice that these variables don't have the dollar sign in front of them, like Bash variables normally do.
With basic arithmetic, you can use the dollar sign with a variable, but not with incrementation. We can also use test operators to determine whether things are equal or if they differ in value. These use different operators than you might be accustomed to. Instead of writing equals, greater than, and so on, we use dash and two letters to represent these conditions. So for equal we would write -eq, for not equal, we would write -ne, and so on. Then we use them in a construction with single square brackets to check values.
So to use those here at the command line, I'd write if [ 4 -eq 5 We'll check to see if 4 is equal to 5. A closing square bracket, a semicolon, the key word then, and echo "yes". This will echo yes if the test returns true. Then a semicolon, and then fi to close the if statement. No result, so those aren't equal.
I'll recall that and move to the beginning of the line with control a. I'll change eq to ne, for not equal, and press enter again. Those aren't equal. Let's try some others. Again, I'll press up to recall my statement, move to the beginning with control a, and I'll change not equal here to -gt, for greater than. Is 4 greater than 5? It doesn't look like it. Let's change that to less than.
There we go, 4 is less than 5. There are some other operators we can use in this construction, as well, to check various aspects of files or folders. Take a look at the help page for the test command to see them all. I'll create a file here with touch and I'll call it myfile. Then I can see if that file exists with if -e and then the file, and it turns out that that file exists.
Let's recall that and change it a bit. I'll change the name of the file to include a dash and I can see that that file doesn't exist. Okay, let's use this to build a little script that will tell us if a system's root file system has more than, say, about 50 gigabytes free. I'll create a file called spacecheck.sh I'll create a variable called space which will be set to the output of a command df -k for the root file system.
This shows the free and used amount in kilobytes. I'll pipe that into awk where I'll write a little program to take the second line of the output and return the fourth item on that line, which will be the amount of free space on the root file system in kilobytes. Then I'll write if space is less than 50 million, which represents 50 million kilobytes or 50 gigabytes, then echo root has less than 50GB available.
Otherwise, echo root has more than 50GB available. We'll end that with fi. I'll save this file and exit and now let's run the script with bash spacecheck.sh It looks like my root file system has more than 50GB free. It's been a quick look and if you'd like to learn more about Bash scripting, be sure to check out our courses that cover it.