1、命令行参数
向shell脚本传递数据的最基本方法是使用命令行参数。命令行参数允许在运行脚本时向命令行添加数据。
bash shell中有一种特殊的变量叫——位置参数,位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2是第二个参数,依次类推,直到第九个参数$9。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
echo "The zero parameter is set to : $0"
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
The zero parameter is set to : test.sh
- 1
- 2
- 3
- 4
- 5
但是这里存在一些问题,如果使用另一个命令来运行shell脚本,命令会和脚本名混在
一起,出现在$0参数中。或者说,当我们以绝对路径调用时,也会返回绝对路径。
[root@relay3.mobvista.com:101.251.254.6 shell]#./test.sh
The zero parameter is set to : ./test.sh
[root@relay3.mobvista.com:101.251.254.6 shell]#sh /root/shell/test.sh
The zero parameter is set to : /root/shell/test.sh
- 1
- 2
- 3
- 4
- 5
要编写一个根据脚本名来执行不同功能的脚本时,我们需要显示单纯的脚本名,而basename命令可以做到,它会返回不包含路径的脚本名。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
name=$(basename $0)
echo "The zero parameter is set to : $name"
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
The zero parameter is set to : test.sh
[root@relay3.mobvista.com:101.251.254.6 shell]#sh /root/shell/test.sh
The zero parameter is set to : test.sh
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
当脚本需要的命令行参数不止9个时,必须在变量数字周围加上花括号,比如${10}表示第十个参数。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
total=$[ ${10} * ${11} ]
echo "The tenth is ${10}"
echo "The eleventh is ${11}"
echo "The total is $total"
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh 0 1 2 3 4 5 6 7 8 9 10
The tenth is 9
The eleventh is 10
The total is 90
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
此外还需注意的是,当脚本认为参数变量中会有数据而实际上并没有时,脚本很有可能会产生错误消息。像下面这样:
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
var1=$1
var2=$2
sum=$[ $1 + $2 ]
echo "Hello,$sum"
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh 7
test.sh: line 4: 7 + : syntax error: operand expected (error token is "+ ")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
为了避免上述情况,我们在使用参数前一定要检查其中是否存在数据。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
if [ -n "$1" ]
then
echo "Hello,$1"
else
echo "Sorry"
fi
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
Sorry
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh Miya
Hello,Miya
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2、特殊参数变量
(1)$#
前面,我们使用过-n测试来检查命令行参数$1中是否有数据。那么,新的问题又来了,当脚本需要多个参数时,我们一个个测试未免过于麻烦,其实我们只要统计一下脚本需要的参数个数即可。变量$#含有脚本运行时携带的命令行参数的个数。可以在脚本中任何地方使用这个特殊变量,就跟普通变量一样。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
name=$(basename $0)
if [ $# -ne 2 ]
then
echo Usage: $name a b
else
sum=$[ $1 + $2 ]
echo $sum
fi
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh 3 6
9
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
Usage: test.sh a b
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
(2) ${!#}
还有一个特殊变量${!#},它代表了最后一个命令行参数变量。如下示例,当我们在运行脚本带命令行参数时,${!#}显示的是最后一个命令行参数,而当我们不带参数时会返回脚本名。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
echo The last is ${!#}
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh 1 2 3
The last is 3
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
The last is test.sh
- 1
- 2
- 3
- 4
- 5
- 6
- 7
(3) $* 和 $@
$*和 $@这两个变量可以用来轻松访问所有的参数。但他们略有不同:
$@变量会将命令行上提供的所有参数当作同一字符串中的多个独立的单词。
$*变量会将命令行上提供的所有参数当作一个单词保存。这个单词包含了命令行中出现的每一个参数值。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
echo "Test \$@ : $@"
echo "Test \$* : $*"
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh cathy vivian miya andy
Test $@ : cathy vivian miya andy
Test $* : cathy vivian miya andy
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
这样看起来好像并无不同,我们试着用for遍历一下看看
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
echo "Test \$@ : $@"
count1=1
for num in "$@"
do
echo "$count1=$num "
count1=$[ $count1 + 1 ]
done
echo " "
echo “Test $* : ∗"count2=1fornumin"*"
count2=1
for num in "∗"count2=1fornumin"*”
do
echo “count2=count2=count2=num”
count2=$[ $count2 + 1 ]
done
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh cathy vivian miya andy
Test $@ : cathy vivian miya andy
1=cathy
2=vivian
3=miya
4=andy
Test $* : cathy vivian miya andy
1=cathy vivian miya andy
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
3、移动变量
shift命令会根据命令行参数的相对位置来对其进行移动。使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置。这是一个帮助我们遍历的好方法。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
count=1
while [ -n "$1" ]
do
echo "$count=$1"
count=$[ $count + 1 ]
shift
done
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh cathy vivian miya andy
1=cathy
2=vivian
3=miya
4=andy
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
这个脚本通过测试第一个参数值的长度执行了一个while循环。当第一个参数的长度为零
时,循环结束。测试完第一个参数后,shift命令会将所有参数的位置移动一个位置。我们也可以给shift指定参数,表示要移动的位置数。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
count=1
while [ -n "$1" ]
do
echo "$count=$1"
count=$[ $count + 1 ]
shift 2 //每次移动两个位置
done
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh cathy vivian miya andy
1=cathy
2=miya
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
好像一切都好,那我们加个参数再试一次
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh cathy vivian miya andy coco
1=cathy
2=miya
3=coco
4=coco
5=coco
6=coco
7=coco
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
为什么会出现这种情况,这就要我们认真分析一下shift命令的执行了。
第一次执行时,$1=cathy,$2=vivian,$3=miya,$4=andy,$5=coco。
进入while,执行了shift=2,这时$1=miya,$2=andy,$3=coco。
再次循环,执行shift=2,$1=coco,这时问题出现了[ -n $1 ]条件永远成立,造成死循环。
4、read
为了得到更好的交互性,我们可以使用read命令。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
read -p "Please input your name:" name
echo "Welcome $name"
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
Please input your name:miya
Welcome miya
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
当我们不进行输入时,read会一直等待,为避免这种情况,我们使用计时器,到时自动退出。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
read -t 5 -p "Please input your name:" name
echo "Welcome $name"
- 1
- 2
- 3
- 4
我们也可以让read命令来统计输入的字符数。当输入的字符达到预设的字符数时,就自动退出,将输入的数据赋给变量。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
read -t 5 -n1 -p "Please input your answer(y/n):" answer
case $answer in
Y|y)
echo
echo " Let's conntinue";;
N|n)
echo
echo " OK, Bye";;
esac
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
Please input your answer(y/n):y
Let's conntinue
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
-s选项可以避免在read命令中输入的数据出现在显示器上(实际上,数据会被显示,只是
read命令会将文本颜色设成跟背景色一样)。
[root@relay3.mobvista.com:101.251.254.6 shell]#cat test.sh
#!/bin/bash
read -s -t 5 -n1 -p "Please input your answer(y/n):" answer
case $answer in
Y|y)
echo
echo " Let's conntinue";;
N|n)
echo
echo " OK, Bye";;
esac
[root@relay3.mobvista.com:101.251.254.6 shell]#sh test.sh
Please input your answer(y/n):
OK, Bye
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15