Mini-Class: Shell Scripting, Day 4

Yesterday you learned how to add comments to your scripts.  You also learned it’s a good practice to start your scripts with a comment that describes why the shell script exists.

You can think of that opening comment, or header, as the goal of the script.  One of the things that can help you write shell scripts is to think about all the steps it takes to accomplish that goal and then create a comment for each step.

These comments are called pseudocode.  It’s like creating an outline of your script before you start scripting.

Let’s try it.

Create Your Next Script

First, create a script named sysinfo.sh:

$ nano sysinfo.sh

Remember, to start your script with a shebang.  Next, use the following as the goal of your script:

# This script displays information about the system on which it is executed.

Write Your Pseudocode

Now you think of how to accomplish that goal.  In this example, you would think about all the information you want to display.  Let’s say you come up with the following list.  Create a comment in your script for each one.

# Tell the user the script is starting.

# Display the hostname of the system.

# Display the current date and time when this information was collected.

# Display the kernel release followed by the architecture.

# Display the disk usage in a human readable format.

# End the script by letting the user know that it’s done.

Write Your Real Code

Now that you have your shebang, header, and pseudocode, it’s time to start filling in the code.

The first comment is “Tell the user the script is starting.”  You already know that you can use the echo command display information to the screen, so you come up with this:

# Tell the user the script is starting.
echo "Starting the sysinfo script."

The next comment is “Display the hostname of the system.”  You can accomplish that task by using the “hostname” command:

# Display the hostname of the system.
hostname

Next, you’re going to display the date and time.  That’s accomplished with the date command:

# Display the current date and time when this information was collected.
date

To display information about the Linux kernel, you can use the “uname” command.  Use the “-r” option to display the kernel release and the “-m” option to display the architecture or machine hardware name:

# Display the kernel release followed by the architecture.
uname -r
uname -m

You can think of the “df” command as standing for disk free.  It’s used to display disk usage and with the “-h” option, it display information in a human readable format:

# Display the disk usage in a human readable format.
df -h

Finally, you want to display that the script is complete:

# End the script by letting the user know that it’s done.
echo "Stopping the sysinfo script."

Put It All Together

Here’s what the “sysinfo.sh” script looks like now:

#!/bin/bash

# This script displays information about the system on which it is executed.

# Tell the user the script is starting.
echo "Starting the sysinfo script."

# Display the hostname of the system.
hostname

# Display the current date and time when this information was collected.
date

# Display the kernel release followed by the architecture.
uname -r
uname -m

# Display the disk usage in a human readable format.
df -h

# End the script by letting the user know that it’s done.
echo "Stopping the sysinfo script."

Run It!

Give your script execute permissions and run it:

$ chmod +x sysinfo.sh
$ ./sysinfo.sh

The output should look similar to this:

Starting the sysinfo script.
web01
Thu Apr 19 10:55:13 EDT 2018
3.10.0-693.17.1.el7.x86_64
x86_64
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/vg00-lv_root   35G  1.9G   31G   6% /
devtmpfs                  234M     0  234M   0% /dev
tmpfs                     245M     0  245M   0% /dev/shm
tmpfs                     245M  4.4M  240M   2% /run
tmpfs                     245M     0  245M   0% /sys/fs/cgroup
/dev/sda1                 976M  124M  785M  14% /boot
tmpfs                      49M     0   49M   0% /run/user/1000
Stopping the sysinfo script.

The more complex scripts you write, the more important pseudocoding becomes.  It allows you to write amazingly complex scripts by simply breaking them down into easy-to-script pieces.

Here’s What To Expect Next…

That’s all for today. In tomorrow’s lesson you’re going to learn about shell builtin variables and how to make decisions in your scripts.

All the best,

Jason

P.S. If you’d like to get started automating all your tasks with shell scripts, you don’t have to wait for the rest of this course to do it; check this out.