Americas

  • United States
sandra_henrystocker
Unix Dweeb

Getting started with scripting on Linux: Testing variables

How-To
Jan 04, 20245 mins
Linux

When writing scripts, it’s important to know how to test and validate variables.

Profile side photo of concentrated skilled web expert afro american girl sit evening desk use computer netbook work java script html back-end algorithm in workstation workplace
Credit: Roman Samborskyi / Shutterstock

This post follows up on Part 1 by examining the many ways that you can test the value of variables – e.g., whether they equal particular strings like “yes” or “no”, if they have a numeric value, if they are null (empty), or if they are larger or smaller than some value or variable.

Testing variables

There are quite a few ways to test the values of variables, but the operators you need to use depend on whether you’re comparing numbers or strings. This post includes a couple scripts that run through a series of comparisons for both numbers and strings to illustrate how they work.

Comparing numbers

To test the value of numbers, you can use any of the tests shown in the script below (e.g., -eq tests for equality). The script asks for you to enter two numbers and then runs the series of tests.

#!/bin/bash

echo -n "Enter first number: "
read num1
echo -n "Enter second number: "
read num2
echo

if [ $num1 -eq $num2 ]; then echo $num1 and $num2 are equal; fi
if [ $num1 -ne $num2 ]; then echo $num1 and $num2 are not equal; fi
if [ $num1 -lt $num2 ]; then echo $num1 is less than $num2; fi
if [ $num1 -le $num2 ]; then echo $num1 is less than or equal $num2; fi
if [ $num1 -gt $num2 ]; then echo $num1 is greater than $num2; fi
if [ $num1 -ge $num2 ]; then echo $num1 is greater than or equal $num2; fi
# use (( and )) for these tests
if (($num1 <= $num2)); then echo $num1 is less than or equal $num2; fi
if (($num1 >= $num2)); then echo $num1 is greater than or equal $num2; 
fi

If you run this script, you can compare any two numbers and see the result for each test that passes. Here’s an example:

Enter first number: 11
Enter second number: 7

11 and 7 are not equal
11 is greater than 7
11 is greater than or equal 7
11 is greater than or equal 7

The results above are somewhat redundant, but only because there is more than one way to run certain tests like greater than or equal to.

Comparing strings

To compare two strings in a script, you would generally use a command like this one:

if [ $ans == "yes" ]; then
    echo "OK, let’s get going"
else
    exit
fi

There are, however, quite a few tests that you can run – whether strings are the same or not and even how they relate alphabetically. The script below compares two strings in many ways.

#!/bin/bash

echo -n "Enter first string: "
read str1
echo -n "Enter second string: "
read str2
echo

# run tests to see how strings relate
if [ "$str1" = "$str2" ]; then echo strings are the same; fi
if [ "$str1" == "$str2" ]; then echo strings are the same; fi
if [ "$str1" != "$str2" ]; then echo \"$str1\" and \"$str2\" differ; fi
if [ "$str1" \< "$str2" ]; then echo \"$str1\" comes first alphabetically; fi
if [ "$str1" \> "$str2" ]; then echo \"$str2\" comes first alphabetically; fi
if [ -z "$str1" ]; then echo string1 is null; fi
if [ -z "$str2" ]; then echo string2 is null; fi
if [ -n "$str1" ]; then echo string1 is not null; fi
if [ -n "$str2" ]; then echo string2 is not null; fi

In some of the tests above, \ characters are inserted before quotes to ensure that the strings are displayed within quote marks.

Here’s a script that uses this test.

#!/bin/bash

echo -n "enter a year&gt; "
read year
this_year=`date +%Y`			# get current year using the date command

if [ "$year" -lt $this_year ]; then
    diff=`expr $this_year - $year`
    echo "Wow, that was $diff year{s} ago"
elif [ "$year" -gt $this_year ]; then
    diff=`expr $year - $this_year`
    echo "Only $diff year(s) to go"
else
    echo "$year is current year"
fi

Notice that the script above uses if, else if and else logic instead of several independent if tests.

You can also try to collect mis-entered data with commands like these that will loop until a proper year is entered.

#!/bin/bash

echo -n "Enter year&gt; "
read year

re='^[0-9]+\$'
while ! [[ $year =~ $re ]]
do
    echo "error: Year must be numeric"
    echo -n "year&gt; "
    read year
done

The examples above are fairly advanced. They set up a numeric expression that will match any string of digits. If it doesn’t match (the “!” in the script means “not”), an error will be displayed and the script will continue looping until it does.

Wrap-up

When writing scripts, it’s important to know how to test and validate variables. You may need to verify that required arguments are provided and that the values match what is needed. Knowing how to compose the tests is an important element of script writing.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.