2001-07-10 14:25:50 +00:00
|
|
|
#!/bin/bash
|
2008-07-20 23:16:47 +00:00
|
|
|
# and list
|
2001-07-10 14:25:50 +00:00
|
|
|
|
2014-07-05 13:38:40 +00:00
|
|
|
if [ ! -z "$1" ] && echo "Argument #1 = $1" && [ ! -z "$2" ] && \
|
2008-11-23 22:43:47 +00:00
|
|
|
# ^^ ^^ ^^
|
|
|
|
echo "Argument #2 = $2"
|
2001-07-10 14:25:50 +00:00
|
|
|
then
|
2001-09-04 13:27:31 +00:00
|
|
|
echo "At least 2 arguments passed to script."
|
2001-07-10 14:25:50 +00:00
|
|
|
# All the chained commands return true.
|
|
|
|
else
|
2008-11-23 22:43:47 +00:00
|
|
|
echo "Fewer than 2 arguments passed to script."
|
2001-07-10 14:25:50 +00:00
|
|
|
# At least one of the chained commands returns false.
|
|
|
|
fi
|
2008-11-23 22:43:47 +00:00
|
|
|
# Note that "if [ ! -z $1 ]" works, but its alleged equivalent,
|
|
|
|
# "if [ -n $1 ]" does not.
|
2005-05-08 20:09:31 +00:00
|
|
|
# However, quoting fixes this.
|
2008-11-23 22:43:47 +00:00
|
|
|
# if "[ -n "$1" ]" works.
|
|
|
|
# ^ ^ Careful!
|
|
|
|
# It is always best to QUOTE the variables being tested.
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
|
2001-09-04 13:27:31 +00:00
|
|
|
# This accomplishes the same thing, using "pure" if/then statements.
|
2001-07-10 14:25:50 +00:00
|
|
|
if [ ! -z "$1" ]
|
|
|
|
then
|
|
|
|
echo "Argument #1 = $1"
|
|
|
|
fi
|
|
|
|
if [ ! -z "$2" ]
|
|
|
|
then
|
|
|
|
echo "Argument #2 = $2"
|
2001-09-04 13:27:31 +00:00
|
|
|
echo "At least 2 arguments passed to script."
|
2001-07-10 14:25:50 +00:00
|
|
|
else
|
2008-11-23 22:43:47 +00:00
|
|
|
echo "Fewer than 2 arguments passed to script."
|
2001-07-10 14:25:50 +00:00
|
|
|
fi
|
2008-11-23 22:43:47 +00:00
|
|
|
# It's longer and more ponderous than using an "and list".
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
|
2008-11-23 22:43:47 +00:00
|
|
|
exit $?
|