getopts 可选参数_linux shell命令行选项与参数用法详解--getopts、getopt

在bash中,可以用以下三种方式来处理命令行参数:

直接处理:使用$1,$2,...,$n进行解析

getopts:单个字符选项的情况(如:-n 10 -f file.txt等选项)

getopt:可以处理单个字符选项,也可以处理长选项long-option(如:--prefix=/home等)

总结:小脚本直接处理即可,getopts能处理绝大多数的情况,getopt较复杂、功能也更强大。

1、直接处理

使用以下几个变量进行处理:

$0 #即命令本身,相当于c/c++中的argv[0]

$1 #第一个参数

$2, $3, $4 ... #第2、3、4个参数,依次类推

$# #参数的个数,不包括命令本身

$@ #参数本身的列表,不包括命令本身

$* #和$@相同,但"$*"和"$@"(加引号)并不同,

#"$*"将所有的参数解释成一个字符串,而"$@"是一个参数数组

2、getopts

说明:

getopts是bash的内部命令

getopts有两个参数,第一个参数是一个字符串,包括字符和“:”

每一个字符都是一个有效的选项(option),如果字符后面带有“:”,表示这个选项有自己的argument,argument保存在内置变量OPTARG中

$OPTIND总是存储原始$*中下一个要处理的元素位置

对于while getopts ":a:bc" opt,第一个冒号表示忽略错误

例如getopts.sh :

#!/bin/bash

echo original parameters=[$*]

echo original OPTIND=[$OPTIND]

while getopts ":a:bc" opt

do

case $opt in

a)

echo "this is -a option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"

;;

b)

echo "this is -b option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"

;;

c)

echo "this is -c option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"

;;

?)

echo "there is unrecognized parameter."

exit 1

;;

esac

done

#通过shift $(($OPTIND - 1))的处理,$*中就只保留了除去选项内容的参数,

#可以在后面的shell程序中进行处理

shift $(($OPTIND - 1))

echo remaining parameters=[$*]

echo \$1=[$1]

echo \$2=[$2]

测试一下:

# ./getopts.sh -a 12 -b -c file1 file2

original parameters=[-a 12 -b -c file1 file2]

original OPTIND=[1]

this is -a option. OPTARG=[12] OPTIND=[3]

this is -b option. OPTARG=[] OPTIND=[4]

this is -c option. OPTARG=[] OPTIND=[5]

remaining parameters=[file1 file2]

$1=[file1]

$2=[file2]

3、getopt

说明:

getopt是一个外部命令,不是bash内置命令,Linux发行版通常会自带

getopt支持短选项和长选项

老版本的getopt问题较多,增强版getopt比较好用,执行命令getopt -T; echo $?,如果输出4,则代表是增强版的

如果短选项带argument且参数可选时,argument必须紧贴选项,如-carg 而不能是-c arg

如果长选项带argument且参数可选时,argument和选项之间用“=”,如--clong=arg而不能是--clong arg

例如getopt.sh:

#!/bin/bash

echo original parameters=[$@]

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

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

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

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

ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -n "$0" -- "$@"`

if [ $? != 0 ]; then

echo "Terminating..."

exit 1

fi

echo ARGS=[$ARGS]

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

eval set -- "${ARGS}"

echo formatted parameters=[$@]

while true

do

case "$1" in

-a|--along)

echo "Option a";

shift

;;

-b|--blong)

echo "Option b, argument $2";

shift 2

;;

-c|--clong)

case "$2" in

"")

echo "Option c, no argument";

shift 2

;;

*)

echo "Option c, argument $2";

shift 2;

;;

esac

;;

--)

shift

break

;;

*)

echo "Internal error!"

exit 1

;;

esac

done

#处理剩余的参数

echo remaining parameters=[$@]

echo \$1=[$1]

echo \$2=[$2]

测试一下:

#短选项

# ./getopt.sh -a -b1 -c2 file1 file2

original parameters=[-a -b1 -c2 file1 file2]

ARGS=[ -a -b '1' -c '2' -- 'file1' 'file2']

formatted parameters=[-a -b 1 -c 2 -- file1 file2]

Option a

Option b, argument 1

Option c, argument 2

remaining parameters=[file1 file2]

$1=[file1]

$2=[file2]

#长选项

./getopt.sh --along --blong=1 --clong=2 file1 file2

original parameters=[--along --blong=1 --clong=2 file1 file2]

ARGS=[ --along --blong '1' --clong '2' -- 'file1' 'file2']

formatted parameters=[--along --blong 1 --clong 2 -- file1 file2]

Option a

Option b, argument 1

Option c, argument 2

remaining parameters=[file1 file2]

$1=[file1]

$2=[file2]

#长短混合

# ./getopt.sh -a -b1 --clong=2 file1 file2

original parameters=[-a -b1 --clong=2 file1 file2]

ARGS=[ -a -b '1' --clong '2' -- 'file1' 'file2']

formatted parameters=[-a -b 1 --clong 2 -- file1 file2]

Option a

Option b, argument 1

Option c, argument 2

remaining parameters=[file1 file2]

$1=[file1]

$2=[file2]

对于可选参数出错的情况:

#短选项和所带argument中间含有空格

# ./getopt.sh -a -b 1 -c 2 file1 file2

original parameters=[-a -b 1 -c 2 file1 file2]

ARGS=[ -a -b '1' -c '' -- '2' 'file1' 'file2']

formatted parameters=[-a -b 1 -c -- 2 file1 file2]

Option a

Option b, argument 1

Option c, no argument

remaining parameters=[2 file1 file2]

$1=[2]

$2=[file1]

#长选项和所带argument中间含有空格

# ./getopt.sh --along --blong 1 --clong 2 file1 file2

original parameters=[--along --blong 1 --clong 2 file1 file2]

ARGS=[ --along --blong '1' --clong '' -- '2' 'file1' 'file2']

formatted parameters=[--along --blong 1 --clong -- 2 file1 file2]

Option a

Option b, argument 1

Option c, no argument

remaining parameters=[2 file1 file2]

$1=[2]

$2=[file1]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值