Mini-Class: Shell Scripting, Day 5

Upgrade to the Full Shell Scripting Course

If you’re ready to take things to the next level, upgrade to the complete shell scripting course by clicking here.

Here are just a few things you’ll learn when you enroll:

  • Create random data so you can do things like automatically generate strong passwords for user accounts.
  • Perform the same action or set of actions over a series of data utilizing for loops, while loops, and infinite loops.
  • Control all types of input and output.
  • Perform text and string manipulation.
  • Process command line arguments.
  • Creating functions and when to do so.
  • Parsing, analyzing, and reporting on log files, CSV files, and other data.
  • Writing scripts that execute commands on other systems.
  • Scheduling your scripts to run at specific times.
  • Monitoring services and processes.
  • Automatically restart failed services.
  • And much more…

I hope to see you in the course.

All the best,

Jason


DAY 5 LESSON NOTES:

Making decisions is what you do as a human.  Every day you’re confronted with many choices and it’s up to you to choose the right one.

When you start automating your work with shell scripts, you have to put your good decision-making abilities into your scripts.

One way to do this is by using if statements. In real life, you use an “if framework” to make decisions. For example, if something is good for you, you do it.  If it’s not, you don’t.

Write a Script

Let’s create a script that displays if the user running the script is using root privileges.

$ nano got-root.sh

Type the following contents into the file. Be very careful to use the same spacing, case, and punctuation as shown below:

#!/bin/bash

# Determine if the user executing this script is the root user or not.

# Display the UID
echo "Your UID is ${UID}."

# Display if the user is the root user or not.
if [[ "${UID}" -eq 0 ]]
then
  echo "You are root."
else
  echo "You are not root."
fi

Builtin Variables

This script uses the builtin command “echo” to display information to the screen.  In addition to builtin commands, bash gives you builtin variables too.  One of those variables is “UID” which contains the user ID of the person executing the script.

In your script, you use the value of “UID” to perform a test and make a decision.  By the way, the UID of the root is always “0”.  That’s why we can use it to see if a user is root or not.

The if Statement

The syntax of an if statement is:

if [[ TEST ]]
then
  COMMANDS
fi

When the TEST is true, the COMMANDS that follow the “then” keyword executed.  If the TEST is not true, then no COMMANDS are executed and the script continues after the “fi” keyword.

The syntax of an if/else statement is:

if [[ TEST ]]
then
  COMMANDS
else
  COMMANDS
fi

Here, if the TEST is true the COMMANDS that follow the “then” keyword executed. If the TEST is not true, the commands following the “else” keyword are executed.

It’s important to point out that you must use at least one space after the “if” keyword and at least one space after the opening double brackets.  You also must use at least one space before the closing double bracket.

This is the correct syntax:

if [[ "${UID}" -eq 0 ]]

This will give you an error.  (Do NOT do the following.)

if [["${UID}" -eq 0]]

This test is performing an equality check.  In plain English it read, “If the user ID is equal to 0.”  So, “-eq” represents equal.  You use it to compare numbers.  Here are the other arithmetic test operators:

-eq equal
-ne not equal
-lt less than
-gt greater than
-ge greater than or equal

Use the equal sign (=) to compare strings (or a series of letters).

Single Brackets Versus Double Brackets (Old vs. New)

If you studied shell scripting in the past or just looked at some sample shell scripts, you might have seen if statements that used single brackets.  Like this:

if [ "${UID}" -eq 0 ]
then
  echo "You are root."
else
  echo "You are not root."
fi

The double bracket syntax you learned about above is the latest and preferred syntax.  Said another way, it replaced the single bracket syntax.  However, you’ll still see old scripts written this way and new scripts written by people who haven’t caught up with the times. You’re ahead of them now. 😉

The main advantage of using double brackets is that it comes with less surprises.  It does what you think it does.  It does what you want.

You can run into issues when using single brackets, especially around quoting variables, filename expansion, and using operators.  (We haven’t covered those topics yet…)

Run Your Script

Now you understand what your script does.  Try it out:

$ chmod +x got-root.sh
$ ./got-root.sh

If you are using a normal user account you will see this output:

You are not root.

If you are using the root account you will see this output:

You are root.

One way to execute the script as the root user is to use the sudo command.  (NOTE: You may or may not have sudo privileges depending on the specific configuration of your system.)

The root user is also known as the superuser since it can do anything.  Sudo stands for “super user do.”

$ sudo ./got-root.sh
You are root.

(NOTE: If sudo isn’t configured on your system you will see an error message like “you are not in the sudoers file.”)

Another way to execute the script as the root user is to first switch to the root account.  You can do that with “su” command which stands for “switch user.”  When you execute “su,” you will be prompted for root’s password.  Once you’re root, execute the script.

$ su
Password:
# ./got-root.sh
You are root.

To log out of the root account and return to your normal user, type “exit.”

# exit

Putting It into Practice

It’s a good idea to check for any requirements that your shell script depends on in the script itself.  For example, if you write a script that installs software you want to make sure the person executing the script is using root privileges because root privileges are required to install software.

Here’s an example you can use in that script:

if [[ "${UID}" -eq 0 ]]
then
  echo "Installing software."
  # The commands to install software go here.
else
  echo "You do not have permissions to install software."
fi

What You’ve Learned Today

In today’s lesson you learned how to control the execution of your scripts by using if statements.  You also learned about one of the many shell builtin variables.

Here’s What To Do Next…

I hope you enjoyed this mini crash course into Linux shell scripting!

You did great and you’re off to an awesome start.

But as you know, you just barely scratched the surface of what’s possible with shell scripting.

If you’re ready to take your skills to the master level, I’ve got just the thing for you.

It’s called “Master Linux Shell Scripting in Just 20 Hours.”

It’s designed so that you learn 90% of everything you’ll ever need to know about shell scripting in the shortest time possible.

If you can spare just  5 hours a week, you’ll be done in just 4 weeks…

Imagine, 4 weeks from now you are writing complicated shell scripts with ease.

You automate all the work you hate doing and you are getting more done than you ever thought possible.

Any script you dream up, you write.

That would be great, right?

If that’s the future you want, enroll now.

Jason