LDP/LDP/guide/docbook/abs-guide/match-string.sh

39 lines
586 B
Bash
Raw Normal View History

2001-10-15 14:28:18 +00:00
#!/bin/bash
2012-11-27 14:56:18 +00:00
# match-string.sh: Simple string matching
# using a 'case' construct.
2001-10-15 14:28:18 +00:00
match_string ()
2008-07-20 23:16:47 +00:00
{ # Exact string match.
2001-10-15 14:28:18 +00:00
MATCH=0
2008-07-20 23:16:47 +00:00
E_NOMATCH=90
2001-10-15 14:28:18 +00:00
PARAMS=2 # Function requires 2 arguments.
2008-07-20 23:16:47 +00:00
E_BAD_PARAMS=91
2001-10-15 14:28:18 +00:00
2008-07-20 23:16:47 +00:00
[ $# -eq $PARAMS ] || return $E_BAD_PARAMS
2001-10-15 14:28:18 +00:00
case "$1" in
"$2") return $MATCH;;
2008-07-20 23:16:47 +00:00
* ) return $E_NOMATCH;;
2001-10-15 14:28:18 +00:00
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