[CentOS] bash - safely pass untrusted strings?
Jacques B.
jjrboucher at gmail.com
Wed Feb 27 16:57:04 UTC 2008
Here's a little script that I have to play around with positional
parameters. I'm pretty certain I did not author this one but got it
either off the web or ina book. I added a line of comment in it but
I don't believe I made any other contributions to it.
Jacques B.
#!/bin/bash
# arglist.sh
# Invoke this script with several arguments, such as
# ./scriptname one "two three" four "five;six\" seven eight 'nine ten'
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` argument1 argument2 etc."
exit $E_BADARGS
fi
echo
index=1 # Initialize count.
echo "Listing args with \"\$*\":"
for arg in "$*" # Doesn't work properly if "$*" isn't quoted.
do
echo "Arg #$index = $arg"
let "index+=1"
done # $* sees all arguments as single word.
echo "Entire arg list seen as single word."
echo
index=1 # Reset count.
# What happens if you forget to do this?
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done # $@ sees arguments as separate words.
echo "Arg list seen as separate words."
echo
index=1 # Reset count.
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."
exit 0
More information about the CentOS
mailing list