shell脚本编程-处理用户输入
命令行参数
- 命令行参数:允许在运行脚本时向命令行添加数据值
如:$ ./addem 10 30
读取参数
- bash shell会将一些称为位置参数的特殊变量分配给命令行输入的所有参数,甚至包括shell执行的程序的名字
- 位置参数变量是标准的数字
- $0:程序名
- $1:第一个参数
- 2:第二个参数,以此类推,直到第9个参数9
- 位置参数变量是标准的数字
$cat test
#!/bin/bash
total=$[ $1 * $2 ]
echo The total value is $total.
$./test 2 5
The total value is 10.
- shell参数可以是字符串,每个参数都是用空格分隔的,所以shell会将空格当成分隔两个值的分隔符
- 参数值中包含空格,必须要用引号(单引号双引号都可以)
|
1
2
3
4
5
6
7
|
#cat test
#!/bin/bash
echo Hello $1, glad to meet you
$./test 'Rich Blum'
Hello Rich Blum, glad to meet you.
$
|
- 脚本需要多于9个命令行参数时,需要使用花括号{},比如,${10}
读取程序名
- $0:获取shell在命令行启动的程序的名字
- 当传给$0变量的真实字符串是整个脚本的路径时,程序中就会使用整个路径,而不仅仅是程序名
- basename命令会只返回程序名而不包括路径
|
1
2
3
4
5
6
7
8
9
10
11
12
|
$cat test
#!/bin/bash
name=`basename $0`
echo The command entered is : $0
echo The command entered is : $name
$./test
The command entered is : ./test
The command entered is : test
$/home/rich/test
The command entered is : /home/rich/test
The command entered is : test
$
|
测试参数
- 当脚本认为参数变量中有数据而实际上并没有时,会得到一个错误
- 解决方法:在使用参数前检查参数[ -n “$1” ]
特殊参数变量
参数计数
- $#特殊变量:含有脚本运行时就有的命令行参数的个数,可以在脚本中任何地方使用,跟普通变量一样
- 最后一个参数的表示形式是{!#}而不是{$#}
- 当命令行上没有任何参数时,#的值为0,在params变量中也为0,但{!#}变量返回命令行用到的脚本名
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$cat test
#!/bin/bash
if [ $# -ne 2 ]
then
echo Usage: test a b
else
total=$[ $1 + $2 ]
echo The total is $total
fi
$./test
Usage: test a b
$./test 10
Usage: test a b
$./test 10 15
The total is 25
$./test 10 15 20
Usage: test a b
|
抓取所有的数据
- ∗和@变量提供了对所有参数的快速访问,这两个都能在单个变量中存储所有的命令行参数
- $*:会将命令行上提供的所有参数当做单个单词保存,即当成一个参数
- $@:将命令行上提供的所有参数当做同一字符串中的多个独立的单词,允许遍历所有的值,将提供的每个参数分隔开来,通常用for命令完成
移动变量
- shift命令:根据它们的相对位置来移动命令行参数
- 默认情况下会将每个参数变量减1,所以变量3的值会移动到2,2的值会移动到1,而变量$1的值会被删除
- shift命令可以提供一个参数n来执行多位移动,如shift 2:连续移动2位
- 当一个参数被移除后,它的值会被丢掉无法恢复
- 变量$0的值,也就是程序名不会改变
- 可以用shift命令遍历命令行参数,尤其在不知道到底有多少个参数的时候
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$cat test
#!/bin/bash
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
$
$./test rich barbara
parameter #1 = rich
Parameter #2 = barbara
|
处理选项
- 选项:跟在单破折线后面的单个字母,能改变命令的行为(如: ls -a)
查找选项
- 在命令行上,选项紧跟在脚本名之后,就跟命令行参数一样
- 处理简单选项:在提取参数时,用case语句来判断参数是否被格式化成了选项
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$cat test
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b option";;
*) echo "$1 is not an option";;
esac
shift
done
$
$./test -a -c
Found the -a option
-c is not an option
$
|
- 分离参数和选项
- shell会用双破折线来表明选项结束了,遇到双破折线之后,脚本会安全地将剩下的命令行参数当做参数来处理,而不是选项
|
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
|
$cat test
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b 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
$
$./test -a test1
Found the -a option
test1 is not an option
$./test -a -- test1
Found the -a option
Parameter #1: test1
|
- 处理带值的选项
- 当命令行选项要求额外的参数时,脚本必须能检测并能正确的处理
|
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
29
30
31
32
33
34
35
|
$cat test
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift 2;;
-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
$
$./test -a -b test1 -d
Found the -a option
Found the -b option, with parameter value test1
-d is not an option
$
$./test -b test1 -a -d
Found the -b option, with parameter value test1
Found the -a option
-d is not an option
$
|
- 如果将多个选项放进一个参数中时,它就不工作了
|
1
2
3
|
$ ./test -ac
-<span class="ruby">ac is <span class="hljs-keyword">not</span> an option
</span>$
|
使用getopt命令
- getopt:识别命令行,从而在脚本中解析它们时更方便
- 命令格式:getopt options optstring parameters
- optstring:定义了命令行有效的选项字母,还定义了哪些选项字母需要参数值
- 首先,在optstring中列出你要在脚本中用到的每个命令行选项字母
- 然后,在每个需要参数值的选项字母后加一个冒号
- optstring:定义了命令行有效的选项字母,还定义了哪些选项字母需要参数值
- getopt命令会给予你定义的optstring解析提供的参数
- 命令格式:getopt options optstring parameters
|
1
2
3
|
$getopt ab:cd -a -b test1 -cd test2 test3
-a -b test1 -c -d -- test2 test3
$
|
- 如果指定了一个不在optstring中的选项,默认情况下,getopt命令会产生一条错误信息
- 如果想忽略这条错误,在命令后加上-q选项
|
1
2
3
4
5
6
7
|
$getopt ab:cd -a -b test1 -cde test2 test3
getopt: invail option --e
-a -b test1 -c -d -- test2 test3
$
$getopt -q ab:cd -a -b test1 -cde test2 test3
-a -b test1 -c -d -- test2 test3
$
|
- 在脚本中使用getopts
- 可以在脚本中使用getopt来格式化输入给脚本的任何命令行选项
- 方法:
- 首先,用getopt命令生成的格式化后的版本来替换已有的命令行选项和参数,用set命令可以做到
- set命令的选项之一是双破折号,它会将命令行参数替换成set命令的命令行的值
- 然后,该方法将原始的脚本命令行参数传递给getopt命令
- 之后再将getopt命令的输出传给set命令,用getopt格式化后的命令行参数来替换原始的命令行参数
- 首先,用getopt命令生成的格式化后的版本来替换已有的命令行选项和参数,用set命令可以做到
- 看起来如下:
set —
getopts -q ab:cd "$@"
Shell
|
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
29
30
31
32
|
$cat test
#!/bin/bash
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-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
$
$./test -a -b test1 -cd test2 test3
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2'
Parameter #1: 'test3'
|
shell脚本编程-处理用户输入的更多相关文章
- shell脚本,提示用户输入一个用户名,如果存在;显示用户UID和SHELL信息;否则,则显示无此用户;显示完成之后,提示用户再次输入;如果是quit则退出;
[root@localhost wyb]# cat tishiuser.sh #!/bin/bash #提示用户输入一个用户名,如果存在:显示用户UID和SHELL信息:否则, #则显示无此用户:显示 ...
- shell编程中用户输入处理(shell 04)
shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...
- Shell脚本编程30分钟入门
Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- 关于shell脚本编程的10个最佳实践
每一个在UNIX/Linux上工作的程序员可能都擅长shell脚本编程.但大家解决问题的方式却不尽相同,这要取决于对专业知识的掌握程度.使 用命令的种类.看待问题的方式等等.对于那些处在shell脚本 ...
- Shell脚本编程具体解释
第12章 Shell脚本编程 l Shell命令行的执行 l 编写.改动权限和运行Shell程序的步骤 l 在Shell程序中使用參数和变量 l 表达式比較.循环结构语句和条件结构语句 l ...
- 30分钟快速学习Shell脚本编程
什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...
- Shell脚本编程学习入门 02
Shell脚本编程学习入门是本文要介绍的内容,我们可以使用任意一种文字编辑器,比如gedit.kedit.emacs.vi等来编写shell脚本,它必须以如下行开始(必须放在文件的第一行): #! ...
- Shell脚本编程学习入门 01
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...
随机推荐
- Spring整合Quartz实现持久化、动态设定时间
一.spring整合 网上一搜有很多整合的方式,这里我采用了其中的一种(暂时还没有对其他的方法研究过). 对于spring的整合其中的任务,spring提供了几个类.接口(这些类都实现了Job接口): ...
- SQLLite 可以通过SQL语言来访问的文件型SQL数据库
Web Storage分为两类: - sessionStorage:数据保存在session 对象中(临时) - localStorage:数据保存在本地硬件设备中(永久) sessionStorag ...
- 为什么java里用常量赋值就相等,用字符串就不等?
例一: String s0="HF"; String s1=new String("HF"); System.out.println(s0==s1); 输入为什 ...
- OpenCV学习笔记——图像的腐蚀与膨胀
顺便又复习了一下cvcopy如何进行图像拼接(最近觉得打开多幅图像分别看不如缩小掉放拼接到一幅图像上对比来的好) 首先把拼接的目标图像设置兴趣区域ROI,比如我有一个total,要把a.b.c分别从左 ...
- ci文件缓存使用,可以用来实现多模板切换 改写URL辅助函数
//2015年2月28日15:13:41 $this->load->driver('cache', array('adapter' => 'file'));//加载适配器 //请注意 ...
- 小红伞和virtualbox5.0.10冲突
win7 sp1 64bit 旗舰版:virtual box 5.0.10 提示 error in supr3hardNtChildWaitFor……Timed out after 60001 ms ...
- Nginx 笔记与总结(2)信号控制
开启.关闭.重启 Nginx 官方地址:http://wiki.nginx.org/CommandLine 开启: /usr/local/nginx/sbin/nginx 关闭,用信号控制,语法: k ...
- mysq数据库再次理解
1.表中的一条记录就是一个object,object有很多属性,对应表中的字段.object的属性对应的值就是字段值 2.外键是关联表关系用的.表关系的确立只能通过外键 但更高效的策略是,在数据库中部 ...
- Spring Project Annotations
Project Annotation Discovered By Package Target(s) Parameters Notes . AspectJ @EnableSpr ...
- PHP开发绝对不能违背的安全铁则
PHP开发绝对不能违背的安全铁则 [来源] 达内 [编辑] 达内 [时间]2012-12-27 使用 mysql_real_escape_string() 作为用户输入的包装器,就可以避免用 ...