November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

shell string comparison tests

shell string comparison tests
Here are the operators for performing string comparison tests:

s1 Test if s1 is not the empty string
s1 = s2 Test if s1 equals s2
s1 != s2 Test if s1 is not equal to s2
-n s1 Test if s1 has non-zero size
-z s1 Test if s1 has zero size
Here’s an example of how to see if two strings are equal:

if [ $foo = $bar ]
then
# do something
fi
This script echoes TRUE:

s1=

if [ -n $s1 ]
then
echo “TRUE”
else
echo “FALSE”
fi
This script echoes FALSE:

s1=bar

if [ -z “$s1” ]
then
echo “TRUE”
else
echo “FALSE”
fi

Linux shell script math/number equality tests
Here’s how you perform math/number/arithmetic tests using the Bourne and Bash shells:

n1 -eq n2 Test if n1 equals n2
n1 -ne n2 Test if n1 is not equal to n2
n1 -lt n2 Test if n1 is less than n2
n1 -le n2 Test if n1 is less than or equal to n2
n1 -gt n2 Test if n1 is greater than n2
n1 -ge n2 Test if n1 is greater than or equal to n2
Here’s an example of how to test whether two numbers are equal:

if [ $n1 -eq $n2 ]
then
# do something
fi
Linux shell boolean and/or/not operators
The following boolean and/or/not operators can also be used in your tests:

-a and
-o or
! not
Here’s an example of how to test perform a test using the and operator:

if [ $num -gt 0 -a $num -lt 10 ]
then
# do something here
fi

Linux shell script math/number equality tests
Here’s how you perform math/number/arithmetic tests using the Bourne and Bash shells:

n1 -eq n2 Test if n1 equals n2
n1 -ne n2 Test if n1 is not equal to n2
n1 -lt n2 Test if n1 is less than n2
n1 -le n2 Test if n1 is less than or equal to n2
n1 -gt n2 Test if n1 is greater than n2
n1 -ge n2 Test if n1 is greater than or equal to n2
Here’s an example of how to test whether two numbers are equal:

if [ $n1 -eq $n2 ]
then
# do something
fi
Linux shell boolean and/or/not operators
The following boolean and/or/not operators can also be used in your tests:

-a and
-o or
! not
Here’s an example of how to test perform a test using the and operator:

if [ $num -gt 0 -a $num -lt 10 ]
then
# do something here
fi

a=5
b=20

if test \( $a -gt 0 -a $a -lt 10 \) -o \( $b -gt 0 -a $b -lt 20 \)
then
echo “TRUE”
else
echo “FALSE”
fi
That script echoes “TRUE”.

Linux Bourne shell if, then, else, else if (elif) syntax
One thing that varies from one programming language to another is the if / then / else / else if / elseif syntax. In the case of the Bourne shell, the “else if” keyword is actually “elif”, so a sample Bourne shell if then else if statement looks like this:

if [ -e ‘foo’ ]
then
echo “if was true”
elif [ -e ‘bar’ ]
then
echo “elif was true”
else
echo “came down to else”
fi

Linux Bourne shell arithmetic
In the Bourne shell math/arithmetic is performed using the expr command, like this:

sum=`expr $foo + $bar`
half=`expr $foo / 2`
times=`expr $foo \* 2`

# increment a counter
(( count++ ))
Note that you can’t have any spaces before or after the equal sign in those (or any) shell script assignment statements.

A few other common Linux shell tricks
Here are a few other tricks/techniques you will often see in Unix shell scripts:

cmd1 && cmd2 Run cmd1; if it returns 0 (success), run cmd2
cmd1 || cmd2 Run cmd1; if it returns non-zero, run cmd2
cmd1 & cmd2 Run cmd1 and also cmd2
(ls -1) Run the command “ls -1” in a subshell

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>