位置参数
$0, $1, $2,等等...位置参数,从命令行传递给脚本,或者是传递给函数.或者赋职给一个变量.
(具体见Example 4-5 和Example 11-15)
$#
命令行或者是位置参数的个数.(见Example 33-2)
$*
所有的位置参数,被作为一个单词.
注意:"$*"必须被""引用.
$@
与$*同义,但是每个参数都是一个独立的""引用字串,这就意味着参数被完整地传递,
并没有被解释和扩展.这也意味着,每个参数列表中的每个参数都被当成一个独立的
单词.
注意:"$@"必须被引用.
具体见代码:
#!/bin/bash
# arglist.sh
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` argument1 argument2 etc."
exit $E_BADARGS
fi
echo
index=1
echo "Listing args with \"\$*\":"
for arg in "$*"
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Entire arg list seen as single word."
echo
index=1
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Arg list seen as separate words."
echo
index=1
echo "Listing args with \$* (unquoted):"
for arg in $*
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Arg list seen as separate words."
exit 0
# arglist.sh
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` argument1 argument2 etc."
exit $E_BADARGS
fi
echo
index=1
echo "Listing args with \"\$*\":"
for arg in "$*"
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Entire arg list seen as single word."
echo
index=1
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Arg list seen as separate words."
echo
index=1
echo "Listing args with \$* (unquoted):"
for arg in $*
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Arg list seen as separate words."
exit 0
#!/bin/bash
echo 'begin work '
binHome=`dirname $0`
echo $binHome
cd $binHome
binHome=`pwd`
echo $binHome
workHome=${binHome}/..
echo $workHome
b=${workHome}/common/date.sh
echo $b
source ${workHome}/common/date.sh
index=1
for arg in $*
do
if [[ "x$arg" == "x" ]]; then
exit 1
else
case $index in
1)
loadDate=$arg
;;
2)
days=$arg
;;
*)
execFile=$arg
execDate=${loadDate}
for ((i=0;i<days;i=i+1))
do
bash ${execFile} ${execDate}
execDate=`dashDateAdd ${execDate} '1'`
done
;;
esac
let "index+=1"
fi
done
exit 0
echo 'begin work '
binHome=`dirname $0`
echo $binHome
cd $binHome
binHome=`pwd`
echo $binHome
workHome=${binHome}/..
echo $workHome
b=${workHome}/common/date.sh
echo $b
source ${workHome}/common/date.sh
index=1
for arg in $*
do
if [[ "x$arg" == "x" ]]; then
exit 1
else
case $index in
1)
loadDate=$arg
;;
2)
days=$arg
;;
*)
execFile=$arg
execDate=${loadDate}
for ((i=0;i<days;i=i+1))
do
bash ${execFile} ${execDate}
execDate=`dashDateAdd ${execDate} '1'`
done
;;
esac
let "index+=1"
fi
done
exit 0