shell脚本编程--处理用户输入

本篇内容均摘自《Linux命令行与shell脚本编程大全》,个人认为需要重点学习的章节。【免费】Linux命令行与Shell脚本编程大全 第3版 PDF全本 21MB 百度网盘下载 - 今夕是何夕 - 博客园
bash shell提供了一些不同的方法来从用户处获得数据,包括命令行参数(添加在命令后的数据)、命令行选项(可修改命令行为的单个字母)以及直接从键盘读取输入的能力。本章将会讨论如何在你的bash shell脚本运用这些方法来从脚本用户处获得数据。

14.1 命令行参数
向shell脚本传递数据的最基本方法是使用命令行参数。命令行参数允许在运行脚本时向命令行添加数据。

$ ./addem 10 30

本例向脚本addem传递了两个命令行参数( 10和30)。脚本会通过特殊的变量来处理命令行参数。后面几节将会介绍如何在bash shell脚本中使用命令行参数。
14.1.1 读取参数
bash shell会将一些称为位置参数( positional parameter)的特殊变量分配给输入到命令行中的所有参数。这也包括shell所执行的脚本名称。位置参数变量是标准的数字: 0是程序名, 1是第一个参数, 2是第二个参数,依次类推,直到第九个参数$9。下面是在shell脚本中使用单个命令行参数的简单例子:

$ cat test1.sh
#!/bin/bash
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
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
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.

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

$ cat test4.sh
#!/bin/bash
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

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

14.1.2 读取脚本名
可以用$0参数获取shell在命令行启动的脚本名。

$ cat test5.sh
#!/bin/bash
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
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
name=$(basename $0)
if [ $name = "addem" ]
then
  total=$[ $1 + $2 ]
  elif [ $name = "multem" ]
    then
      total=$[ $1 * $2 ]
 fi
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),另一个通过链接(参见第3章)创建( multem)。在两种情况下都会先获得脚本的基本名称,然后根据该值执行相应的功能。

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

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

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

$ cat test7.sh
#!/bin/bash
if [ -n "$1" ]
then
  echo Hello $1, glad to meet you.
else
  echo "Sorry, you did not identify yourself. "
fi
$ ./test7.sh Rich
Hello Rich, glad to meet you.

$ ./test7.sh
Sorry, you did not identify yourself.

在本例中,使用了-n测试来检查命令行参数$1中是否有数据。

你可能感兴趣的:(shell脚本编程--处理用户输入)