LDP/LDP/guide/docbook/abs-guide/arglist.sh

51 lines
1.0 KiB
Bash
Raw Permalink Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2003-09-15 14:30:43 +00:00
# arglist.sh
2012-11-27 14:56:18 +00:00
# Invoke this script with several arguments, such as "one two three" ...
2001-07-10 14:25:50 +00:00
2012-11-27 14:56:18 +00:00
E_BADARGS=85
2001-09-04 13:27:31 +00:00
2001-07-10 14:25:50 +00:00
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` argument1 argument2 etc."
2001-09-04 13:27:31 +00:00
exit $E_BADARGS
2001-07-10 14:25:50 +00:00
fi
echo
2003-11-03 16:25:16 +00:00
index=1 # Initialize count.
2001-07-10 14:25:50 +00:00
echo "Listing args with \"\$*\":"
for arg in "$*" # Doesn't work properly if "$*" isn't quoted.
do
echo "Arg #$index = $arg"
let "index+=1"
2001-09-04 13:27:31 +00:00
done # $* sees all arguments as single word.
2001-07-10 14:25:50 +00:00
echo "Entire arg list seen as single word."
echo
2003-11-03 16:25:16 +00:00
index=1 # Reset count.
# What happens if you forget to do this?
2001-07-10 14:25:50 +00:00
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
2001-09-04 13:27:31 +00:00
done # $@ sees arguments as separate words.
2001-07-10 14:25:50 +00:00
echo "Arg list seen as separate words."
echo
2003-11-03 16:25:16 +00:00
index=1 # Reset count.
2003-09-15 14:30:43 +00:00
echo "Listing args with \$* (unquoted):"
for arg in $*
do
echo "Arg #$index = $arg"
let "index+=1"
done # Unquoted $* sees arguments as separate words.
echo "Arg list seen as separate words."
2001-07-10 14:25:50 +00:00
exit 0