mirror of https://github.com/tLDP/LDP
39 lines
586 B
Bash
39 lines
586 B
Bash
#!/bin/bash
|
|
# match-string.sh: Simple string matching
|
|
# using a 'case' construct.
|
|
|
|
match_string ()
|
|
{ # Exact string match.
|
|
MATCH=0
|
|
E_NOMATCH=90
|
|
PARAMS=2 # Function requires 2 arguments.
|
|
E_BAD_PARAMS=91
|
|
|
|
[ $# -eq $PARAMS ] || return $E_BAD_PARAMS
|
|
|
|
case "$1" in
|
|
"$2") return $MATCH;;
|
|
* ) return $E_NOMATCH;;
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
a=one
|
|
b=two
|
|
c=three
|
|
d=two
|
|
|
|
|
|
match_string $a # wrong number of parameters
|
|
echo $? # 91
|
|
|
|
match_string $a $b # no match
|
|
echo $? # 90
|
|
|
|
match_string $b $d # match
|
|
echo $? # 0
|
|
|
|
|
|
exit 0
|