next up previous contents
Next: Combining test: Expressions Up: test: Comparisons Previous: Verifying File Types   Contents

Comparing Strings

String comparisons are done using $=$ and !$=$:
    #!/bin/sh
    if test $# -eq 0
    then
        echo Must provide parameters.
        exit 1
    fi
    
    while test ! $1 = "end"
    do
        echo parameter is $1
        shift
        if test $# -eq 0
        then
            echo Parameter list MUST contain the '"'end'"' string.
            exit
        fi
    done
    echo Done: I"'"ve hit the '"'end'"' string.
    exit 0
Note that the above example could have been MUCH shorter if no error checking took place.

The length of strings can also be tested using:

-z
: check if the string has zero length.
-n
: check if the string has a non-zero length.



Claude Cantin 2010-10-24