LDP/LDP/guide/docbook/abs-guide/and-or.sh

63 lines
1.1 KiB
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
a=24
b=47
if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
2001-07-10 14:25:50 +00:00
then
echo "Test #1 succeeds."
else
echo "Test #1 fails."
fi
# ERROR: if [ "$a" -eq 24 && "$b" -eq 47 ]
2004-01-26 00:03:37 +00:00
#+ attempts to execute ' [ "$a" -eq 24 '
#+ and fails to finding matching ']'.
2001-07-10 14:25:50 +00:00
#
# Note: if [[ $a -eq 24 && $b -eq 24 ]] works.
2004-01-26 00:03:37 +00:00
# The double-bracket if-test is more flexible
#+ than the single-bracket version.
# (The "&&" has a different meaning in line 17 than in line 6.)
2004-01-26 00:03:37 +00:00
# Thanks, Stephane Chazelas, for pointing this out.
2001-07-10 14:25:50 +00:00
if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]
then
echo "Test #2 succeeds."
else
echo "Test #2 fails."
fi
2001-09-04 13:27:31 +00:00
# The -a and -o options provide
#+ an alternative compound condition test.
# Thanks to Patrick Callahan for pointing this out.
2001-07-10 14:25:50 +00:00
if [ "$a" -eq 24 -a "$b" -eq 47 ]
then
echo "Test #3 succeeds."
else
echo "Test #3 fails."
fi
if [ "$a" -eq 98 -o "$b" -eq 47 ]
then
echo "Test #4 succeeds."
else
echo "Test #4 fails."
fi
a=rhino
b=crocodile
if [ "$a" = rhino ] && [ "$b" = crocodile ]
2001-07-10 14:25:50 +00:00
then
echo "Test #5 succeeds."
else
echo "Test #5 fails."
fi
exit 0