shell除了局部变量,环境变量之外,还有特殊变量。如$0,$1,$2等等。

代码如下:

#!/bin/bash
#

echo "the name of this script is $0"
#
if [ -n "$1" ]
then
echo "the first paramenter is $1"
else
echo "the first paramenter is NULL"
fi
#
if [ -n "$2" ]
then
echo "the second paramenter is $2"
else
echo "the second paramenter is NULL"
fi
#
if [ -n "$3" ]
then
echo "the third parmenter is $3"
else
echo "the third parmenter is NULL"
fi
#
echo
echo "all the command_line paramenters arg is: $*"
exit 0

运行结果如下:

yz@debian:~/shelltest$ ./test_s1.sh aaa bbb 
the name of this script is ./test_s1.sh
the first paramenter is aaa
the second paramenter is bbb
the third parmenter is NULL

all the command_line paramenters arg is: aaa bbb

解释:

if [ -n "$1" ]是判断命令行第1个参数,在这里是aaa是不是为空。

$1,$2,$3......是命令行第1,2,3......个参数,超过10个,则需要用{},即${10},${11},${12}等等来表示。

*是通配符,$*就表示所有的$n。