命令行参数
#./admin 10 30
读取参数
[root@localhost scrips]# vi test28.sh
#!/bin/bash
factorial=1
for ((number=1; number<=$1; number++))
do
factorial=$[ $factorial * $number ] #factorial=$(($factorial * $number))
done
echo "Then factorial of $1 is $factorial"
[root@localhost scrips]# bash test28.sh 5
Then factorial of 5 is 120
[root@localhost scrips]# vi test29.sh
#!/bin/bash
total=$(($1*$2))
echo "The first parameter is $1."
echo "The second parameter is $2."
echo "The total value is $total"
[root@localhost scrips]# bash test29.sh 2 5
The first parameter is 2.
The second parameter is 5.
The total value is 10
如果脚本需要的命令行参数不止个,你仍然可以处理,但是需要稍微修改一下变量名,在第9个变量之后,你必须在变量数字周围上花括号,经如${10}。
读取脚本名
#echo "The zero parameter is set to: $0" #$0是读取脚本名用的变量。
[root@localhost scrips]# vi test1a.sh
#!/bin/bash
name=$(basename $0)
echo
echo "The script name is: $name"
[root@localhost scrips]# bash test1a.sh
The script name is: test1a.sh
测试参数
[root@localhost scrips]# vi test2a.sh
#!/bin/bash
if [ -n "$1" ]
then
echo "Hello $1, glad to meet you."
else
echo "Sorry, you did not identify yourself"
fi
[root@localhost scrips]# bash test2a.sh
Sorry, you did not identify yourself
[root@localhost scrips]# bash test2a.sh 4
Hello 4, glad to meet you.
特殊参数变量
参数统计
$#示例
[root@localhost scrips]# vi file1.sh
#!/bin/bash
echo "There were $# parameters supplied."
[root@localhost scrips]# bash file1.sh 1 2 3 4 5
There were 5 parameters supplied.
[root@localhost scrips]# vi file2.sh
#!/bin/bash
params=$#
echo
echo "The last parameter is $params
echo "The last parameter is ${!#} #变量${!#}会返回命令行用到的脚本名
echo
[root@localhost scrips]# bash file2.sh
The last parameter is 0
echo The last parameter is file2.sh
抓取所有的数据
[root@localhost scrips]# vi file3.sh
#!/bin/bash
echo
echo "Using the $* method: $*"
echo
echo "Using the $@ method: $@"
[root@localhost scrips]# bash file3.sh rich barbara katie jessica
Using the $* method: rich barbara katie jessica
Using the $@ method: rich barbara katie jessica
$*和$@变量可以用来轻松访问所有的参数,这个两个变量都能够在单个变量中存储所有的命令行参数。$*变量会将命令行上提供的所有参数当作一个单词保存,这个单词包含了命令行中出现的每一个参数值。基本上$*变量会将这些参数视为一个整体,而不是多个个体。
$@变量会将命令行上提供的所有参数当作同一个字符串的多个独立的单词,这样你就能够遍历所有的参数值,得到每个参数,
[root@localhost scrips]# vi file3.sh
#!/bin/bash
echo
count=1
for param in "$*"
do
echo "$* parameter $count = $param"
count=$[ $count + 1 ]
done
echo
count=1
for param in "$@"
do
echo "$@ parameter $count = $param"
count=$[ $count + 1 ]
done
[root@localhost scrips]# bash file3.sh rich barbara katie jessica
$* parameter 1 = rich barbara katie jessica
$@ parameter 1 = rich
$@ parameter 2 = barbara
$@ parameter 3 = katie
$@ parameter 4 = jessica
移动变量
[root@localhost scrips]# vi file5.sh
#!/bin/bash
echo
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
[root@localhost scrips]# bash file5.sh 1 2 3 4 5
Parameter #1 = 1
Parameter #2 = 2
Parameter #3 = 3
Parameter #4 = 4
Parameter #5 = 5
在使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置,所以及$3的值会移动到$2中,变量$2的值会移动到$1中,而变量$1的值会被删除(注意:变量$0的值,也就是程序名,不会改变)
处理选项
查找选项
[root@localhost scrips]# vi file8.sh
#!/bin/bash
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done
[root@localhost scrips]# bash file8.sh -a -b -c -d
Found the -a option
Found the -b option
Found the -c option
-d is not an option
case语句会检查每个参数是不是有效选项,如果是的话,就运行对应case语句中的命令,不管选项按什么顺序出现在命令行上,这种方法都适用。
分离参数和选项
[root@localhost scrips]# vi file10.sh
#!/bin/bash
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
[root@localhost scrips]# bash file10.sh -a -b -c test1 test2 test3
Found the -a option
Found the -b option
Found the -c option
test1 is not an option
test2 is not an option
[root@localhost scrips]# bash file10.sh -a -b -c -- test1 test2 test3
Found the -a option
Found the -b option
Found the -c option
Parameter #1: test1
Parameter #2: test2
Parameter #3: test3