十年专注于品牌网站建设 十余年专注于网站建设_小程序开发_APP开发,低调、敢创新、有情怀!
南昌百恒网络微信公众号 扫一扫关注
小程序
tel-icon全国服务热线:400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络微信小程序

百恒网络

南昌百恒网络

linux中如何使用getopt命令?

百恒网络 2017-09-15 6419

getopt命令是一个在处理命令行选项和参数时非常方便的工具。它能够识别命令行参数,从而在脚本中解析它们时更方便。

1、命令的格式

getopt命令可以接受一系列任意形式的命令行选项和参数,并自动将它们转换成适当的格式。它的命令格式如下:

getopt optstring parameters

optstring是这个过程的关键所在。它定义了命令行有效的选项字母,还定义了哪些选项字母需要参数值。 首先,在optstring中列出你要在脚本中用到的每个命令行选项字母。然后,在每个需要参数值的选项字母后加一个冒号。getopt命令会基于你定义的optstring解析提供的参数。

下面南昌网络公司-百恒网络为大家举一个getopt工作的简单例子,让大家可以更好的了解它。

$ getopt ab:cd -a -b test1 -cd test2 test3

-a -b test1 -c -d -- test2 test3

$

optstring定义了四个有效选项字母:a、b、c和d。冒号(:)被放在了字母b后面,因为b选项需要一个参数值。当getopt命令运行时,它会检查提供的参数列表(-a -b test1 -cd test2 test3),并基于提供的optstring进行解析。注意,它会自动将-cd选项分成两个单独的选项,并插入双破折线来分隔行中的额外参数。

如果指定了一个不在optstring中的选项,默认情况下,getopt命令会产生一条错误消息。

$ getopt ab:cd -a -b test1 -cde test2 test3

getopt: invalid option -- e

-a -b test1 -c -d -- test2 test3

$

如果想忽略这条错误消息,可以在命令后加-q选项。

$ getopt -q ab:cd -a -b test1 -cde test2 test3

-a -b 'test1' -c -d -- 'test2' 'test3'

$

注意,getopt命令选项必须出现在optstring之前。现在应该可以在脚本中使用此命令处理命令行选项了。

2、在脚本中使用getopt

可以在脚本中使用getopt来格式化脚本所携带的任何命令行选项或参数,但用起来略微复杂。那么具体如何操作呢?下面南昌网络公司就来和大家探讨一下:

方法是用getopt命令生成的格式化后的版本来替换已有的命令行选项和参数。用set命令能够做到。

set命令的选项之一是双破折线(--),它会将命令行参数替换成set命令的命令行值。

然后,该方法会将原始脚本的命令行参数传给getopt命令,之后再将getopt命令的输出传 给set命令,用getopt格式化后的命令行参数来替换原始的命令行参数,看起来如下所示。

set -- $(getopt -q ab:cd "$@")

现在原始的命令行参数变量的值会被getopt命令的输出替换,而getopt已经为我们格式化好了命令行参数。

利用该方法,现在就可以写出能帮我们处理命令行参数的脚本。

$ cat test18.sh

#!/bin/bash

# Extract command line options & values with getopt

#

set -- $(getopt -q ab:cd "$@")

#

echo

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

#

$

你会注意到它跟脚本test17.sh一样,唯一不同的是加入了getopt命令来帮助格式化命令行参数。

现在如果运行带有复杂选项的脚本,就可以看出效果更好了。

$ ./test18.sh -ac

Found the -a option

Found the -c option

$

当然,之前的功能照样没有问题。

$ ./test18.sh -a -b test1 -cd test2 test3 test4

Found the -a option

Found the -b option, with parameter value 'test1'

Found the -c option

Parameter #1: 'test2'

Parameter #2: 'test3'

Parameter #3: 'test4'

$

现在看起来相当不错了。但是,在getopt命令中仍然隐藏着一个小问题。看看这个例子。

$ ./test18.sh -a -b test1 -cd "test2 test3" test4

Found the -a option

Found the -b option, with parameter value 'test1'

Found the -c option

Parameter #1: 'test2

Parameter #2: test3'

Parameter #3: 'test4'

$

getopt命令并不擅长处理带空格和引号的参数值。它会将空格当作参数分隔符,而不是根据双引号将二者当作一个参数。幸好还有另外一个办法能解决这个问题。

以上便是南昌网络公司-百恒网络为大家介绍的关于linux中getopt命令的使用方法,希望能够对大家有所帮助。此外,本公司专业从事网站建设、微信开发、APP开发等服务,如有需要,欢迎大家来电咨询,洽谈合作!


400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络小程序

欢迎您的光顾,我们将竭诚为您服务×

售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售后服务 售后服务
 
售后服务 售后服务
 
备案专线 备案专线
 
×