OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

A More Modern Version Of test

Recent versions of bash include a compound command that acts as an enhanced replace- ment for test. It uses the following syntax:

[[ expression ]]

where, like test, expression is an expression that evaluates to either a true or false re- sult. The [[ ]] command is very similar to test (it supports all of its expressions), but


adds an important new string expression:

string1 =~ regex

which returns true if string1 is matched by the extended regular expression regex. This opens up a lot of possibilities for performing such tasks as data validation. In our earlier example of the integer expressions, the script would fail if the constant INT contained anything except an integer. The script needs a way to verify that the constant contains an integer. Using [[ ]] with the =~ string expression operator, we could improve the script this way:



#!/bin/bash


# test-integer2: evaluate the value of an integer.


INT=-5


if [[ "$INT" =~ ^-?[0-9]+$ ]]; then

if [ $INT -eq 0 ]; then echo "INT is zero."

else

if [ $INT -lt 0 ]; then echo "INT is negative."

else

echo "INT is positive."

fi

if [ $((INT % 2)) -eq 0 ]; then echo "INT is even."

else

echo "INT is odd."

fi

fi

else

echo "INT is not an integer." >&2 exit 1

fi

#!/bin/bash


# test-integer2: evaluate the value of an integer.


INT=-5


if [[ "$INT" =~ ^-?[0-9]+$ ]]; then

if [ $INT -eq 0 ]; then echo "INT is zero."

else

if [ $INT -lt 0 ]; then echo "INT is negative."

else

echo "INT is positive."

fi

if [ $((INT % 2)) -eq 0 ]; then echo "INT is even."

else

echo "INT is odd."

fi

fi

else

echo "INT is not an integer." >&2 exit 1

fi


By applying the regular expression, we are able to limit the value of INT to only strings that begin with an optional minus sign, followed by one or more numerals. This expres- sion also eliminates the possibility of empty values.

Another added feature of [[ ]] is that the == operator supports pattern matching the same way pathname expansion does. For example:



[me@linuxbox ~]$ FILE=foo.bar

[me@linuxbox ~]$ FILE=foo.bar

A More Modern Version Of test


[me@linuxbox ~]$ if [[ $FILE == foo.* ]]; then

> echo "$FILE matches pattern 'foo.*'"

> fi

foo.bar matches pattern 'foo.*'

[me@linuxbox ~]$ if [[ $FILE == foo.* ]]; then

> echo "$FILE matches pattern 'foo.*'"

> fi

foo.bar matches pattern 'foo.*'


This makes [[ ]] useful for evaluating file and pathnames.


Top OS Cloud Computing at OnWorks: