1、读取参数:位置参数变量是标准的数字: $0是程序名, $1是第一个参数, $2是第二个参数...
#!/bin/bash
# using one command line parameter factorial=
for (( number = ; number <= $; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo The factorial of $ is $factorial
执行:
# ./test1.sh
The factorial of is
2、输入多个命令行选项,则在命令行上每个参数都必须用空格分开:
#!/bin/bash
# testing two command line parameters total=$[ $ * $ ]
echo The first parameter is $
echo The second parameter is $
echo The total value is $total
执行:
# ./test2.sh
The first parameter is
The second parameter is
The total value is
3、如果脚本需要多个9个命令行参数,在变量数字周围加花括号:
#!/bin/bash
# handling lots of parameters total=$[ ${} * ${} ]
echo The tenth parameter is ${}.
echo The eleventh parameter is ${}.
echo The total is $total
执行:
# ./test4.sh
The tenth parameter is .
The eleventh parameter is .
The total is
4、测试参数
#!/bin/bash
# testing parameters before use if [ -n "$1" ] # -n 参数测试 $1(第一个参数)是否为null
then
echo Hello $, glad to meet you.
else
echo "Sorry, you did not identify yourself"
fi
执行:
# ./test7.sh
Sorry, you did not identify yourself ./test7.sh frank
Hello frank, glad to meet you.
5、特殊参数变量(参数计数 $#)
#!/bin/bash
# getting the number of parameters
echo There were $# parameters supplied.
执行:
# ./test8.sh
There were parameters supplied. ./test8.sh 1 2 3
There were 3 parameters supplied.
6、使用参数前测试参数的总数
#!/bin/bash
# testing parameters if [ $# -ne ]
then
echo Usage: test a b
else
total=$[ $ + $ ]
echo The total is $total
fi
执行:
# ./test9.sh
The total is 3 # ./test9.sh
Usage: test a b
7、不需要知道有多少个参数个数,抓取最后一个参数 ${!#}
#!/bin/bash
params=$#
echo The number of parameter is $params
echo The last parameter is ${!#}
执行:
# sh test10.sh
The number of parameter is #参数的总个数
The last parameter is 4 #最后一个参数的值
8、$* $@变量提供了对所有参数的快速访问, $*变量会将命令行上提供的所有参数当作单个单词保存,$@变量会将命令行上提供的所有参数当做同一个字符串中多个独立的词。
#!/bin/bash
# testing $* and $@ echo "Using the \$* method: $*"
echo "Using the \$@ method: $@"
执行:
# ./test11 rich katie jessica
Using the $* method: rich katie jessica
Using the $@ method: rich katie jessica
差异:
#!/bin/bash
# testing $* and $@
count=1 #赋值的时候注意不要有空格
for param in "$*" #将所有参数变为一个参数
do
echo "\$* parameter #$count = $param"
count=$[ $count + ]
done count=
for param in "$@" #各个参数独立
do
echo "\$@ parameter #$count = $param"
count=$[ $count + ]
done
~
执行:
# ./test12.sh rich barbara katie jessica
$* parameter # = rich barbara katie jessica
$@ parameter # = rich
$@ parameter # = barbara
$@ parameter # = katie
$@ parameter # = jessica
可见, $*会将所有的参数当成单个单数,而$@变量会单独处理每个参数。
9、shift的用法,遍历命令行参数
#!/bin/bash
#demonstrating the shift command count=
while [ -n "$1" ] #注意要加冒号
do
echo "parameter #$count = $1"
count=$[ $count + ]
shift #参数变量逐次减一
done
~
执行:
# ./test13.sh a b c d e
parameter # = a
parameter # = b
parameter # = c
parameter # = d
parameter # = e
10、shift命令提供一个参数执行多位移动
#!/bin/bash
# demonstrating a multi-position shift echo "The original parameters: $*"
shift
echo "Here's the new first parameter: $1"
执行:
# ./test14.sh
The original parameters:
Here's the new first parameter: 3