首先getopts命令是bash内建的一个专用于短选项(单字符选项)解析的命令。其基本用法是getopts optstring name [args],因此示例一中的getopts命令与基本用法的各部分的一个对应关系是::hp:t:对应optstring ,opt对应name,-p 10 -t3对应[args]部分。 3)optstring部分 optstring部分是一个由(短)选项字符和冒号所构成...
tcsh # Example input and output (from the bash prompt): # ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long " # Option a # Option c, no argument # Option c, argument `more' # Option b, argument ` very long ' # Remaining arguments: # --> ...
TEMP=`getopt-o ab:c:: --longa-long,b-long:,c-long:: \-n'example.bash'--"$@"`if[ $? !=0] ;thenecho"Terminating...">&2; exit1;fi# Note the quotes around `$TEMP': they are essential!eval set --"$TEMP"whiletrue;docase"$1"in-a|--a-long)echo"Option a";shift;;-b|-...
/bin/bash #echo $@ #-o或--options选项后面接可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的 #-l或--long选项后面接可接受的长选项,用逗号分开,冒号的意义同短选项。 #-n选项后接选项解析错误时提示的脚本名字 ARGS=`get...
/bin/bash# -o表示段选项,--long表示长选项# 选项后面没有冒号表示没有参数,1个冒号表示必须有参数,2个冒号表示可选参数# 对于具有可选参数的选项,没有没有提供值,会返回一个空字符串,并且会占用一个位置TEMP=`getopt -o ab:c:: -l a-long,b-long:,c-long:: -n'example.bash'--"$@"`if[ $?
1、bash 内置的getopts: 先看简单的例子: #!/bin/bashwhile getopts 'd:Dm:f:t:' OPT; docase$OPTind)DEL_DAYS="$OPTARG";;D)DEL_ORIGINAL='yes';;f)DIR_FROM="$OPTARG";;m)MAILDIR_NAME="$OPTARG";;t)DIR_TO="$OPTARG";;?)echo"Usage:`basename$0`[options] filename"esacdoneshift$(($OP...
If there are problems parsing the parameters, for example because a required argument is not found or an option is not recognized, an error will be reported on stderr, there will be no output for the offending element, and a non-zero error status is returned. For a short option, a sing...
getopt.getopt(args, options[, long_options]) 对于短格式options,-号后要紧跟一个选项字母。如果还有此选项的附加参数,可以用空格分开,也可以不分开,长度任意,可以用引号(比如-uroot、-u root、-u"root"、-u’root’、-u “root”、-u ‘root’)。
Getopt::Long是 Perl 语言中的一个模块,用于解析命令行参数。使用这个模块可以方便地处理命令行工具的参数输入。下面是如何使用Getopt::Long来解析并打印命令参数的基础概念和步骤。 基础概念 命令行参数:用户在命令行中输入的参数,通常跟随在程序名称之后。
http://yejinxin.github.io/parse-shell-options-with-getopt-command 三、使用getopts解析参数 getopts比getopt弱一些简单一些:不能在命令行中单独使用、不支持长选项格式、不支持选项值可选。更多说明见注释。 #!/bin/bash usage(){ echo “ Usage: