当前位置:   article > 正文

python getopts_getopts/getopt命令行参数处理

python getopts_getopts/getopt命令行参数处理

一、getopts 简介

由于shell命令行的灵活性,自己编写代码判断时,复杂度会比较高。使用内部命令 getopts 可以很方便地处理命令行参数。

一般格式为:

getopts options variable

getopts 的设计目标是在循环中运行,每次执行循环,getopts 就检查下一个命令行参数,并判断它是否合法。

即检查参数是否以 - 开头,后面跟一个包含在 options 中的字母。如果是,就把匹配的选项字母存在指定的变量 variable 中,

并返回退出状态0;如果 - 后面的字母没有包含在 options 中,就在 variable 中存入一个 ?,并返回退出状态0;

如果命令行中已经没有参数,或者下一个参数不以 - 开头,就返回不为0的退出状态。

二、使用举例

#!/bin/bashwhilegetopts h:ms optiondo

case "$option" inh)

echo"option:h, value $OPTARG"echo"next arg index:$OPTIND";;

m)

echo"option:m"echo"next arg index:$OPTIND";;

s)

echo"option:s"echo"next arg index:$OPTIND";;

\?)

echo"Usage: args [-h n] [-m] [-s]"echo"-h means hours"echo"-m means minutes"echo"-s means seconds"exit1;;

esac

done

echo"*** do something now ***"

张小猪(~) -> ./args -h 100 -ms

option:h, value100next arg index:3option:m

next arg index:3option:s

next arg index:4

*** do something now ***

张小猪(~) -> ./args -t

./args: illegal option --t

Usage: args [-h n] [-m] [-s]-h means hours-m means minutes-s means seconds

注:

1.getopts 允许把选项堆叠在一起(如 -ms)

2.如要带参数,须在对应选项后加 :(如h后需加参数 h:ms)。此时选项和参数之间至少有一个空白字符分隔,这样的选项不能堆叠。

3.如果在需要参数的选项之后没有找到参数,它就在给定的变量中存入 ? ,并向标准错误中写入错误消息。

否则将实际参数写入特殊变量 :OPTARG

4.另外一个特殊变量:OPTIND,反映下一个要处理的参数索引,初值是 1,每次执行 getopts 时都会更新。

三、getopt 简介

利用shell内置的getopts命令来帮助我们处理shell脚本选项和参数,其缺点是只能处理短选项,无法处理长选项。

下面,本文将要介绍的是getopt命令,它可以同时处理短选项和长选项。

首先,getopt命令不是一个标准的unix命令,但它在大多数Linux的发行版中都自带了有,

如果没有,也可以从getopt官网上(http://frodo.looijaard.name/project/getopt)下载安装。

在getopt的较老版本中,存在一些bug,不大好用,在后来的版本中解决了这些问题,我们称之为getopt增强版。

通过-T选项,我们可以检查当前的getopt是否为增强版,返回值为4,则表明是增强版的。

#getopt -T

#echo $?

4#getopt-V

getopt (enhanced)1.1.4

#!/bin/bash

#echo $@

#-o或--options选项后面接可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,

#其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的

#-l或--long选项后面接可接受的长选项,用逗号分开,冒号的意义同短选项。

#-n选项后接选项解析错误时提示的脚本名字

ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -n 'example.sh' -- "$@"`if [ $? != 0]; then

echo"Terminating..."exit1fi

#echo $ARGS

#将规范化后的命令行参数分配至位置参数($1,$2,...)

evalset -- "${ARGS}"

while true

do

case "$1" in

-a|--along)

echo"Option a";

shift

;;-b|--blong)

echo"Option b, argument $2";

shift2;;-c|--clong)case "$2" in

"")

echo"Option c, no argument";

shift2;;*)

echo"Option c, argument $2";

shift2;

;;

esac

;;--)

shiftbreak;;*)

echo"Internal error!"exit1;;

esac

done

#处理剩余的参数for arg in$@doecho"processing $arg"done

#./getopt.sh -b 123 -a -c456 file1 file2

Option b, argument123Option a

Option c, argument456processing file1

processing file2

#./getopt.sh --blong 123 -a --clong=456file1 file2

Option b, argument123Option a

Option c, argument456processing file1

processing file2

四、查找选项

shift命令默认情况下将每个参数变量向左移动一个位置。 $3的值会移动到$2中,$2的值会移动到1中,$1的值则会被删除。

shift n 表示移动的位数

以上脚本通过测试第一个参数值的长度执行了一个while循环,当第一个参数的长度为0时,循环结束,测试完第一个参数后,shift命令会将所有的参数位置移动一个位置。

参考引用:

https://www.jianshu.com/p/cf71bc239739

https://www.cnblogs.com/yaoyaojcy/p/9227272.html

https://blog.csdn.net/nanfeibuyi/article/details/110354570

https://blog.csdn.net/sofia1217/article/details/52244582

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/811983
推荐阅读
相关标签
  

闽ICP备14008679号