处理用户输入

一.命令行参数
       向shell脚本传递数据的最基本方法是使用命令行参数。命令行参数允许在运行脚本时向命令行添加数据。
       $./addem 10 30
       本例向脚本addem传递了两个命令行参数(10和30)。脚本会通过特殊的变量来处理命令行参数。后面几节将会介绍如何在bash shell脚本中使用命令行参数

1.读取参数
       bash shell会将一些成为位置参数的特殊变量分配给输入到命令行中的所有参数。这也包括shell所执行的脚本名称。位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2是第二个参数。一次类推,直到$9.

       下面是在shell脚本中使用单个命令行参数的简单例子。

$ cat test1.sh
#!/bin/bash
# using one command line parameter
#
factorial=1
for (( number = 1; number <= $1 ; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo The factorial of $1 is $factorial
$
$ ./test1.sh 5
The factorial of 5 is 120
$

可以再shell脚本中向使用其他变量一样使用$1变量。shell脚本会自动将命令行参数的值分配变量,不需要你作任何处理.

       如果需要输入更多的命令行参数,则每个参数都必须用空格分开

$ cat test2.sh
#!/bin/bash
# testing two command line parameters
#
total=$[ $1 * $2 ]
echo The first parameter is $1.
echo The second parameter is $2.
echo The total value is $total.
$
$ ./test2.sh 2 5
The first parameter is 2.
The second parameter is 5.
The total value is 10.

shell会将每个参数分配给对应的变量

       在前面的例子中,用到的命令行参数都是数值。也可以在命令行上用文本字符串。

$ cat test3.sh
#!/bin/bash
# testing string parameters
#
echo Hello $1, glad to meet you.
$
$ ./test3.sh Rich
Hello Rich, glad to meet you.
$

       shell将输入到命令行的字符串值传给脚本。单碰到含有空格的文本字符串时就会出现问题:

$ ./test3.sh Rich Blum
Hello Rich, glad to meet you.
$

       记住,每个参数都是用空格分隔的,所以shell会将空格当成两个值的分隔符。要在参数值中包含空格,必须要用引号(单引号或双引号都可以)

$ ./test3.sh 'Rich Blum'
Hello Rich Blum, glad to meet you.
$
$ ./test3.sh "Rich Blum"
Hello Rich Blum, glad to meet you.
$

______________________________________________________________________________________________________
说明将文本字符串作为参数传递时,引号并非数据的一部分。它们只是表明数据的起止位置。
______________________________________________________________________________________________________

       如果脚本需要的命令行参数不止九个,你仍然可以处理,但是需要稍微修改一下变量名。在第九个变量之后,你必须在变量数字周围加上花括号,比如$(10)。下面是一个这样的例子。

$ cat test4.sh
#!/bin/bash
# handling lots of parameters
#
total=$[ ${
   10} * ${
   11} ]
echo The tenth parameter is ${
   10}
echo The eleventh parameter is ${
   11}
echo The total is $total
$
$ ./test4.sh 1 2 3 4 5 6 7 8 9 10 11 12
The tenth parameter is 10
The eleventh parameter is 11
The total is 110
$

这项技术允许你根据需要向脚本添加任意多的命令行参数。

2.读取脚本名
       可以用$0参数获取shell在命令行启动的脚本名。这在编写多功能工具时很方便。

$ cat test5.sh
#!/bin/bash
# Testing the $0 parameter
#
echo The zero parameter is set to: $0
#
$
$ bash test5.sh
The zero parameter is set to: test5.sh
$

但是这里存在一个潜在的问题。如果使用另一个命令来运行shell脚本,命令会和脚本名混在一起,出现在$0参数中。

$ ./test5.sh
The zero parameter is set to: ./test5.sh
$

这还不是唯一的问题。当传给$0变量的实际字符串不仅仅是脚本名,二十完整的脚本路径时,变量$0就会使用整个路径。

$ bash /home/Christine/test5.sh
The zero parameter is set to: /home/Christine/test5.sh
$

       如果你要编写一个根据脚本名来执行不同功能的脚本,就得做点额外工作。你得把脚本的运行路径给剥离掉。另外,你还要删除与脚本名混杂在一起的命令。
       幸好有个方便的小命令可以帮到我们。basename命令会返回不包含路径的脚本名。

$ cat test5b.sh
#!/bin/bash
# Using basename with the $0 parameter
#
name=$(basename $0)
echo
echo The script name is: $name
#
$ bash /home/Christine/test5b.sh
The script name is: test5b.sh
$
$ ./test5b.sh
The script name is: test5b.sh
$

现在好多了。可以用这种方法来编写基于脚本名执行不同功能的脚本。这里有个简单的例子。

$ cat test6.sh
#!/bin/bash
# Testing a Multi-function script
#
name=$(basename $0)
#
if [ $name = "addem" ]
then
total=$[ $1 + $2 ]
#
elif [ $name = "multem" ]
then
total=$[ $1 * $2 ]
fi
#
echo
echo The calculated value is $total
#
$
$ cp test6.sh addem
$ chmod u+x addem
$
$ ln -s test6.sh multem
$
$ ls -l *em
-rwxrw-r--. 1 Christine Christine 224 Jun 30 23:50 addem
lrwxrwxrwx. 1 Christine Christine 8 Jun 30 23:50 multem -> test6.sh
$
$ ./addem 2 5
The calculated value is 7
$
$ ./multem 2 5
The calculated value is 10
$

       本例从test6.sh脚本中创建了两个不同的文件名:一个通过复制文件创建(addem),另一个通过链接创建(multem)。在两种情况下都会先获得脚本的基本名称,然后根据该值执行相应的功能。

3.测试参数
       在shell脚本中使用命令行参数时要小心些。如果脚本不加参数运行,可能会出问题。

$ ./addem 2
./addem: line 8: 2 + : syntax error: operand expected (error
token is " ")
The calculated value is
$

       当脚本认为参数变量中会有数据二实际上并没有时,脚本很有可能胡产生错误消息。这种写脚本的方法并不可取。在使用参数前一定腰间擦其中是否存在数据。

$ cat test7.sh
#!/bin/bash
# testing parameters before use
#
if [ -n "$1" ]
then
echo Hello $1, 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值