Skip to content

Both.org

News, Opinion, Tutorials, and Community for Linux Users and SysAdmins

Primary Menu
  • About Us
  • Computers 101
    • Hardware 101
    • Operating Systems 101
  • End of 10 Events
    • Wake Forest, NC, — 2025-09-20
  • Linux
    • Why I use Linux
    • The real reason we use Linux
  • My Linux Books
    • systemd for Linux SysAdmins
    • Using and Administering Linux – Zero to SysAdmin: 2nd Edition
    • The Linux Philosophy for SysAdmins
    • Linux for Small Business Owners
    • Errata
      • Errata for The Linux Philosophy for SysAdmins
      • Errata for Using and Administering Linux — 1st Edition
      • Errata for Using and Administering Linux — 2nd Edition
  • Open Source Resources
    • What is Open Source?
    • What is Linux?
    • What is Open Source Software?
    • The Open Source Way
  • Write for us
    • Submission and Style guide
    • Advertising statement
  • Downloads
  • Home
  • Programming Bash #8: Initialization and sanity testing
  • Bash
  • Linux
  • Programming

Programming Bash #8: Initialization and sanity testing

David Both December 29, 2023 8 minutes read
OSDC_Penguin_Image_520x292_12324207_0714_mm_v1a
1

In previous articles of this series we created our first, very small, one-line Bash script. We also explored the reasons for creating shell scripts and why they are the most efficient option for the system administrator, rather than compiled programs.We began the task of creating a fairly simple template that we can use as a starting point for other Bash programs. Then we learned how to create and use a simple help function. In order to create our help facility we also learned about using functions and how to handle command line options such as -h.

In this installment we learn more about variables and initializing them as well as how to do a bit of sanity testing to help ensure the program is run under the proper conditions. Remember, our objective is to build working code that will be used for a template for future Bash programming projects. The idea is to make getting started on new programming projects easy by already having common elements available in the template.

Variables

Like all programming languages, the Bash shell can deal with variables. A variable is a symbolic name that refers to a specific location in memory that contains a value of some sort. The value of a variable is changeable, i.e., it is variable. We looked at using variables in my article, Programming Bash #2: Getting Started, so if you are not familiar with using variables you can read that before we go any farther.

Done? Great! Let’s now look at some good practices when using variables.

I always set initial values for every variable used in my scripts. I do this in our template script immediately after the procedures as the first part of the main program body, before processing the options. Initializing each variable with an appropriate value can prevent errors that might occur with uninitialized variables when used in comparison or math operations. Placing this list of variables in one place allows me to see all of the variables that are supposed to be in the script and their initial values.

Our little script so far has only a single variable, $option, so let’s set that. Insert the following lines as shown.

################################################################################
################################################################################
# Main program                                                                 #
################################################################################
################################################################################
# Initialize variables
option=""
################################################################################
# Process the input options. Add options as needed.                            #
################################################################################

Test this to ensure that everything works as it should and that nothing has broken as the result of this change.

Constants

Constants are variables, too – at least they should be. Use variables wherever possible in CLI programs instead of hard-coded values. Even if you think you will only use a particular value once, such as a directory name, a file name, or a text string, create a variable and use the variable where you would have placed the hard-coded name.

For example, the message we print as part of the main body of the program is a string literal, echo “Hello world!”. Let’s change that to a variable. First, add the following statement to the variable initialization section. I like to keep the variable assignments in alphanumeric sorted order to make them easier to find as the list grows longer.

Msg="Hello world!"

And now change the last line of the program from:

echo "Hello world!"

to:

echo "$Msg"

Test the results.

Sanity checks

Sanity checks are simply tests for conditions that need to be true in order for the program to work correctly. For example, the program must be run as the root user or it must run on a particular distribution and release of that distro.

Let’s add a check for root as the running user to our simple program template. Testing for root as the user running the program is easy. When a program runs, it is run as the user that launches it. This is a security strategy designed to prevent users from escalating their privilege level to that of root. The id command can be used to determine the numeric user ID that the program is running under. The id command provides us with several bits of information when used without any options.

[dboth@testvm1 testdir6]$ id
uid=1000(dboth) gid=1000(dboth) groups=1000(dboth),10(wheel)
[dboth@testvm1 testdir6]$ 

Using the -u option just returns the UID of the user. This number is easily usable in our Bash program.

[dboth@testvm1 testdir6]$ id -u
1000
[dboth@testvm1 testdir6]$

Add the following function to the program. I added it after the help procedure but you can place it anywhere in the procedures section. The logic is that if the UID is not zero, which is always the root user’s UID, the program exits.

################################################################################
# Check for root.                                                              #
################################################################################
CheckRoot()
{
   if [ `id -u` != 0 ] 
   then
      echo "ERROR: You must be root user to run this program"
      exit
   fi  
}

Now add a call to the CheckRoot procedure just before the variable initialization. Test this, first running the program as the student user…

[dboth@testvm1 testdir6]$ ./BashTemplate.sh
ERROR: You must be root user to run this program

… and as the root user.

[root@testvm1 testdir6]# ./BashTemplate.sh
Hello world!

We may not always need this particular sanity test, so comment out the call to CheckRoot but leave all the code in place for our template. That way all we need to do in order to use that code in a future program is to uncomment the call.

The code

After making the changes discussed in this article, our code now looks like this.

#!/bin/bash
################################################################################
#                              BashTemplate                                    #
#                                                                              #
# Use this template as the beginning of a new program. Place a short           #
# description of the script here.                                              #
#                                                                              #
# Change History                                                               #
# 11/11/2019  David Both    Original code. This is a template for creating     #
#                           new Bash shell scripts.                            #
#                           Add new history entries as needed.                 #
#                                                                              #
#                                                                              #
################################################################################
################################################################################
################################################################################
#                                                                              #
#  Copyright (C) 2007, 2023 David Both                                         #
#  david@both.org                                                              #
#                                                                              #
#  This program is free software; you can redistribute it and/or modify        #
#  it under the terms of the GNU General Public License as published by        #
#  the Free Software Foundation; either version 2 of the License, or           #
#  (at your option) any later version.                                         #
#                                                                              #
#  This program is distributed in the hope that it will be useful,             #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
#  GNU General Public License for more details.                                #
#                                                                              #
#  You should have received a copy of the GNU General Public License           #
#  along with this program; if not, write to the Free Software                 #
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   #
#                                                                              #
################################################################################
################################################################################

################################################################################
# Help                                                                         #
################################################################################
Help()
{
   # Display Help
   echo "Add description of the script functions here."
   echo
   echo "Syntax: scriptTemplate [-g|h|v|V]"
   echo "options:"
   echo "g     Print the GPL license notification."
   echo "h     Print this Help."
   echo "v     Verbose mode."
   echo "V     Print software version and exit."
   echo
}
################################################################################
# Check for root.                                                              #
################################################################################
CheckRoot()
{
   if [ `id -u` != 0 ]
   then
      echo "ERROR: You must be root user to run this program"
      exit
   fi
}

################################################################################
################################################################################
# Main program                                                                 #
################################################################################
################################################################################

# CheckRoot

# Initialize variables
Msg="Hello world!"
option=""
################################################################################
# Process the input options. Add options as needed.                            #
################################################################################
# Get the options
while getopts ":h" option; do
   case $option in
      h) # display Help
         Help
         exit;;
     \?) # incorrect option
         echo "Error: Invalid option"
         exit;;
   esac
done

echo $Msg

Final exercise

You probably noticed that the Help function in our code refers to some features we have not included in this code. As a final exercise, you should add those functions to the code template we have created.

Summary

In this article we’ve created a couple functions to perform sanity tests for whether the program is running as root and which distro it is running on. Our program is getting a little more complex and so testing is becoming more important and requires more test paths in order to be complete.

By now you get the idea. In this series we have looked at a very minimal Bash program and we have seen how to build a script up a bit at a time. Our end result is a simple template that can be the starting point for other, more useful Bash scripts and which contains some useful elements that make it easy to start new scripts.

Compiled programs are necessary and fill a very important need. But for sysadmins there is always a better way. We should always use shell scripts to meet the automation needs of our jobs. Shell scripts are open; their content and purpose are knowable. They can be readily modified to meet differing requirements. Personally, I have found nothing that I have ever needed to do in my SysAdmin role that could not be accomplished with a shell script.

What we have created so far is just the beginning. As you write more Bash programs of your own, you will find more bits of code that you use frequently and that can be included in your program template.


Series Articles

This list contains links to all eight articles in this series about Bash.

  1. Programming Bash #1 – Introducing a New Series
  2. Programming Bash #2: Getting Started
  3. Programming Bash #3: Logical Operators
  4. Programming Bash #4: Using Loops
  5. Programming Bash #5: Automation with Scripts
  6. Programming Bash #6: Creating a template
  7. Programming Bash #7: Bash Program Needs Help
  8. Programming Bash #8: Initialization and sanity testing
Tags: Bash Programming

Post navigation

Previous: How to encourage positive online communication in your open source community
Next: Future-Proofing Education: Why WordPress is the PK-12 Platform You Need

Related Stories

f44-01-day-cropped
  • Fedora
  • Linux
  • Upgrades

Fedora 44 Released

David Both April 28, 2026
command_line_prompt
  • Command Line
  • Linux
  • Programming

Writing a replacement seq command

Jim Hall April 27, 2026
dark-web
  • Linux
  • News
  • Security
  • Vulnerability

High-severity Vulnerability in PackageKit

David Both April 25, 2026

Random Quote

Fuzzy project objectives are used to avoid the embarrassment of estimating the corresponding costs.

— Laws of computerdom according to Golub

Why I’ve Never Used Windows

On February 12 I gave a presentation at the Triangle Linux Users Group (TriLUG) about why I use Linux and why I’ve never used Windows.

Here’s the link to the video: https://www.youtube.com/live/uCK_haOXPFM 

Why there’s no such thing as AI

Last October at All Things Open (ATO) I was interviewed by Jason Hibbits of We Love Open Source. It’s posted in the article “Why today’s AI isn’t intelligent (yet)“.

Technically We Write — Our Partner Site

Our partner site, Technically We Write, has published a number of articles from several contributors to Both.org. Check them out.

Technically We Write is a community of technical writers, technical editors, copyeditors, web content writers, and all other roles in technical communication.

Subscribe to Both.org

To comment on articles, you must have an account.

Send your desired user ID, first and last name, and an email address for login (this must be the same email address used to register) to subscribe@both.org with “Subscribe” as the subject line.

You’ll receive a confirmation of your subscription with your initial password as soon as we are able to process it.

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

License and AI Statements

Both.org aims to publish everything under a Creative Commons Attribution ShareAlike license. Some items may be published under a different license. You are responsible to verify permissions before reusing content from this website.

The opinions expressed are those of the individual authors, not Both.org.

You may not use this content to train AI.

 

Advertising Statement

Both.org does not sell advertising on this website.


Advertising may keep most websites running—but at Both.org, we’re committed to keeping our corner of the web ad-free. Both.org does not sell advertising on the website. Nor do we offer sponsored articles at this time. We’ll update this page if our position on sponsorships changes.

We want to be open about how the website is funded. Both.org is supported entirely by David Both and a few other dedicated individuals.

 

 

Copyright © All rights reserved. | MoreNews by AF themes.