sarathonline.com/blog
Try my blog on: Ajax Version
Search

Tuesday, January 06, 2009

Numeric and String comparision in Shell

String Comparision is treated differently in Shell. -eq and -ne operators cannot be applied for this. -eq (and the clan) are applicable only when you are comparing numerics. If you have a String comparision, use = (sh) or == (bash).

Examples

##GOOD
#!/bin/sh
if [ "$1" = "set" ]; then
        echo "Setting"
else
        echo "Displaying"
fi

##ERROR
if [ "$1" -eq "set" ]; then
        echo "Setting"
fi
in bash
#!/bin/sh
if [ "$1" == "set" ]; then
        echo "Setting"
fi

0 comments:

Post a Comment

© SarathOnline.com 2000-'09