shell脚本中如何获取命令的参数(2) ----处理命令参数
1 找出选项
1.1 处理简单选项
主要可以通过shfit工具对获取的到$1变量对比程序允许的变量值判断;
1.2 从参数中分离选项
一般参数可能在后面跟上适当的参数值,例如 sed -f script data
所有如何把选项的参数与选项有效的分隔开也是一种很重要的技能,我们可以限制命令后面的规定格式,虽然没有很大的灵活性,但是它却解决了我们的问题,在linux当中用(--)这个特殊字符来表示选项的结束,参数项开始
下面进阶一点,讨论一下如何获取带值的选项
1.3 处理带值的选项
这个步骤主要是让我们脚本能够处理形如:./test -a test1 -b -c -d test2
这就得要求脚本呢必须能够检测命令行选项合适需要附加参数,并能够进行处理,下面的例子可以很好展示如何解决这类问题;
2 使用命令
getopt命令可以接受任意形式的命令行参数,在处理命令行选项和参数时非常方便
命令格式: getopt options optstring parameters
其中选项字符串是处理的关键,它定义命令行中有效选项字母,它还定义哪些选项字母需要参数值
首先,在选项字符串中列出将在脚本中用到的每个命令行选项字母。然后,在每个需要参数值的选项字母后面放置一个冒号。getopt命令根据定义的选项字符串解析提供的参数
请看下面的getopt示例:
$ getopt ab:cd -a -b test1 -cd test2 test3
-a -b test1 -c -d -- test2 test3
$
可见经过getopt的处理得到了规整的参数列表,注意它自动的把-cd 分为-c ,-d ,并插入破折号来分隔行中额外的参数
如果提供的参数不在optstring当中,getopt会给予提示出错信息
jiangjian@jiangjian-K42JZ:~/sh$ getopt ab:cd -a -b test1 -e -cd test2 test3
getopt:无效选项 -- e
-a -b test1 -c -d -- test2 test3
jiangjian@jiangjian-K42JZ:~/sh$
如果希望忽略这个错误信息,可以在命令中使用-q选项
jiangjian@jiangjian-K42JZ:~/sh$ getopt -q ab:cd -a -b test1 -e -cd test2 test3
-a -b 'test1' -c -d -- 'test2' 'test3'
jiangjian@jiangjian-K42JZ:~/sh$
现在介绍如何在脚本中使用getopt
这里存在一个技巧
set命令的一个选项是双破折号,表示将命令行参数变量替换为set命令的命令行中的值
set -- `getopt -q ab:cd "$@"`
按照上面的技巧我们试着编写一个:
jiangjian@jiangjian-K42JZ:~/sh$ cat test60.sh
#!/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";;--) shiftbreak;;*) echo "$1 is not option "esacshift
donecount=1
for param in "$@"
do echo "Parameter #$count:$param"count=$[$count + 1]
done
jiangjian@jiangjian-K42JZ:~/sh$ ./test60.sh -a -b test10 -c -d 1 2 3
Found the -a option
Found the -b option with parameter value 'test10'
Found the -c option
Parameter #1:'1'
Parameter #2:'2'
Parameter #3:'3'
jiangjian@jiangjian-K42JZ:~/sh$
getopts命令
不同于getopt,getopts每次获取参数列表当中的一个合法项,当处理完最后的即返回一个大于0 的退出状态
格式:getopts optstring variable
optstring和getopt大致相同,主要区别在与禁止输出错误信息,这里直接在optstring前面加一个冒号:
其中每次返回的值存放在variable当中,可以用于for循环当中迭代,getopts当中使用两个环境变量,环境变量OPTARG中包含需要参数值选项要使用的值,环境变量OPTIND 包含的值表示getopts停止处理时在参数列表中的位置,这样,处理完选项后可以继续处理其他命令行参数。
jiangjian@jiangjian-K42JZ:~/sh$ cat test61.sh
#!/bin/bashwhile getopts :ab:c opt
docase "$opt" in a) echo "Found -a option";;b) echo "Found -b option,with value $OPTARG";;c) echo "Found -c option";;*) echo "$opt is unknown option";;esac
done
jiangjian@jiangjian-K42JZ:~/sh$ ./test61.sh -a -b test1 -c
Found -a option
Found -b option,with value test1
Found -c option
jiangjian@jiangjian-K42JZ:~/sh$
必须注意上面的case选项中不在包括(-)
getopts会自动处理未定义的参数
jiangjian@jiangjian-K42JZ:~/sh$ ./test61.sh -a -b test1 -c -d
Found -a option
Found -b option,with value test1
Found -c option
? is unknown option
jiangjian@jiangjian-K42JZ:~/sh$
另外getopts知道合适停止处理选项,将参数选项交给您处理。getopts每个处理选项,环境变量OPTIND的值都会增加1,当达到getopts处理的末尾时,可以使用shift命令和OPTIND值进行操作来移动到参数;
实例如下:
jiangjian@jiangjian-K42JZ:~/sh$ cat test62.sh
#!/bin/bash
while getopts :ab:cd opt
docase "$opt" in a) echo "Found the -a option";;b) echo "Found the -b option,with vale $OPTARG";;c) echo "Found the -c option";;d)echo "Found the -d option";;*)echo "$1 is not an option";;esac
done
shift $[$OPTIND - 1]
count=1
for parameter in $@
doecho "Patameter #$count:$parameter"count=$[$count + 1]
done
jiangjian@jiangjian-K42JZ:~/sh$ ./test62.sh -a -b test1 -c -d 1 2 3 4
Found the -a option
Found the -b option,with vale test1
Found the -c option
Found the -d option
Patameter #1:1
Patameter #2:2
Patameter #3:3
Patameter #4:4
jiangjian@jiangjian-K42JZ:~/sh$
shell脚本中如何获取命令的参数(2) ----处理命令参数相关推荐
- shell 脚本中获取执行系统命令的输出结果
这个主要介绍的方法是获取命令的输出内容,而不是命令执行成功与否的返回值. 通常情况下,在shell脚本中需要获取命令的输出内容,然后根据输出内容判断下一步的执行操作. 比较常用的一种方式就是, 匹配命 ...
- shell脚本中export命令未生效,原因详解
1.问题发现 安装 jemalloc 后,执行 /usr/bin/jemalloc.sh 脚本生效环境变量 LD_PRELOAD.执行过后发现环境变量并未生效.过程如下: [root@10-27- ...
- Shell脚本中获取命令运行结果、特殊变量使用、条件判断等常用操作
文章目录 前言 获取命令执行结果 使用反引号 `` ` ``command `` ` ``来执行命令 使用括号组合 $(command) 来执行命令 两种方式的区别 特殊变量使用 $0 $1 $2 . ...
- Shell脚本中的位置参数( $0、$n、${10} ) 、特殊参数( $#、${!#}、$*、$@ )、shift命令和read命令详解
1.命令行参数 向shell脚本传递数据的最基本方法是使用命令行参数.命令行参数允许在运行脚本时向命令行添加数据. bash shell中有一种特殊的变量叫--位置参数,位置参数变量是标准的数字:$0 ...
- linux脚本外输入参数,shell 脚本中关于用户输入参数的处理
1.命令行参数 向shell脚本传递数据的最基本方式是使用命令行参数. (1) 读取参数 读取输入的参数的变量为位置参数,位置参数通过标准数字表示, 其中$0为程序名称,$1为第一个参数,$2为第二个 ...
- 在 Shell 脚本中跟踪调试命令的执行
文章目录 shell 脚本调试系列 概述 shell 跟踪执行的重要性 shell 脚本调试系列 本系列的前面部分清晰地阐明了另外两种 shell 脚本调试模式:详细模式和语法检查模式,并用易于理解的 ...
- shell脚本中执行命令_如何在Shell脚本中执行命令?
shell脚本中执行命令 Shell is a command-line interpreter that allows the user to interact with the system. I ...
- shell获取脚本本身名称_linux,shell脚本中获取脚本的名字,使用脚本的名字。
需求描述: 写shell脚本的过程中,有时会需要获取脚本的名字,比如,有的时候,脚本 中会有usage()这种函数,可能就会用到脚本的名字. 实现方法: shell脚本中,通过使用$0就可以获取到脚本 ...
- Shell脚本中获取本机ip地址,Linux获取本地ip地址
在 Shell 脚本中获取本机 IP 地址可以通过多种方式实现,这里介绍三种常用的方法: 1. 使用 ifconfig 命令获取本机 IP 地址 ifconfig 命令可以获取本机网卡的配置信息,包括 ...
最新文章
- 2021年中国工业互联网安全大赛核能行业赛道writeup之usb流量分析
- python怎么读文件里的某一行-Python如何获取文件指定行的内容
- 记一次Oracle数据故障排除过程
- Linux环境搭建:设置主机名颜色、设置vim颜色
- 计算机网络技术专业发展现状,计算机网络技术的发展现状和前景
- android开发之 Gson 解析json数组
- McAfee Agent漏洞可导致黑客以Windows 系统权限运行代码
- 导出qq群成员信息,并找出谁没进群或谁没改昵称
- 《移动端UI框架》Vant
- waves服务器系统盘,Waves Soundgrid服务器(DIY)
- PHP+Wampserver(其中的MariaDB数据库)制作留言板
- MySQL全网最细总结
- 【GAMES101】课堂笔记1--计算机图形学概述
- windows10蓝牙开关突然不见了,设备管理器中也没有蓝牙标志,也没有感叹号的硬件
- 【Go语言学习】安装与配置
- 转专业申请加拿大计算机硕士,【2015】多伦多大学:转专业、低GPA,一切不再是问题!...
- java代码审计_Java代码审计入门篇
- ntp网络时间服务器(网络时钟同步)客户端与服务器端设置方法说明
- Vue3-浏览器兼容性 IE篇
- Python UI设计学习笔记,第四课:构建左侧菜单,使用 `QPushButton`