Techno World Inc - The Best Technical Encyclopedia Online!

THE TECHNO CLUB [ TECHNOWORLDINC.COM ] => Shell Script => Topic started by: Mark David on March 16, 2007, 01:32:09 PM



Title: Bourne/bash shell scripts: string comparison
Post by: Mark David on March 16, 2007, 01:32:09 PM
Bourne/bash shell scripts: string comparison

To determine if the value of a variable ($var) is empty (null):

Code:
if [ $var == "" ] 
then
  echo variable is null
fi

To determine if the value of a variable is not empty:

Code:
if [ $var != "" ] 
then
  echo variable is not null
fi

To compare the contents of a variable to a fixed string:

Code:
if [ $var == "value" ] 
then
   echo is the same
fi

To determine if variable's contents are not equal to a fixed string:

Code:
if [ $var != "value" ] 
then
  echo not the same
fi