Linux之处理用户输入

mark

命令行参数

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

读取参数

bash shell将一些称为位置参数的特殊变量分配给输入到命令行中的所有参数,包括shell所执行的脚本名称。位置参数变量是标准的数字:$0是程序名,$1是第一个变量,依此类推到第九个参数$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.

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

如果脚本需要的命令行参数不止9个,需要在第九个变量之后的变量数字周围加上花括号,比如${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

读取脚本名

可以用$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

这里存在一个潜在的问题,如果使用另一个命令来运行脚本,命令会和脚本名混在一起,出现在$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
Researcher

I am a PhD student of Crop Genetics and Breeding at the Zhejiang University Crop Science Lab. My research interests covers a range of issues:Population Genetics Evolution and Ecotype Divergence Analysis of Oilseed Rape, Genome-wide Association Study (GWAS) of Agronomic Traits.

comments powered by Disqus