1. getopts是bash内建命令的, 而getopt是外部命令 2. getopts不支持长选项, 比如: --date 3. 在使用getopt的时候, 每处理完一个位置参数后都需要自己shift来跳到下一个位置, getopts只需要在最后使用shift $(($OPTIND - 1))来跳到parameter的位置。 4. 使用getopt时, 在命令行输入的位置参数是什么, 在ge...
-n'example.bash'--"$@"` if[ $?!=0] ; then echo"Terminating...">&2; exit1; fi # Note the quotes around `$TEMP': they are essential! #set会重新排列参数的顺序,也就是改变$1,$2...$n的值,这些值在getopt中重新排列过了 evalset--"$TEMP" #经过getopt的处理,下面处理具体选项。 whil...
getopts 是一个shell内建命令,而且似乎比 getopt 更容易实现这个功能,所以在这篇文章里我准备讲讲getopts。 以前我总想知道如何为我的Bash脚本创建命令行参数。经过搜索,我发现了2个函数可以处理这个问题,getopt 函数和 getopts 函数。我无意争论哪一个函数更好的。getopts 是一个shell内建命令,而且似乎比 getopt ...
/bin/bash# A small example program for using the new getopt(1) program.# This program will only work with bash(1)# An similar program using the tcsh(1) script language can be found# as parse.tcsh# Example input and output (from the bash prompt):# ./parse.bash -a par1 'another a...
运行示例:
3. 使用 getopt 命令 getopt命令是一个强大的工具,可用于解析命令行选项。它可以处理长选项、短选项、多个选项等。以下是一个演示如何使用getopt命令的示例: #!/bin/bashoptions=$(getopt-oab:c--longfoo,bar:,baz::-n'example.sh'--"$@")if[$?-ne0];thenecho"选项错误"exit1fievalset--"$options"wh...
运行示例:
9. getopts 出现的目的是为了代替 getopt 较快捷的执行参数分析工作 下面是getopt自带的一个例子: 代码语言:javascript 复制 Code highlighting produced by ActiproCodeHighlighter(freeware)http://www.CodeHighlighter.com/-->#!/bin/bash #Asmall example programforusing thenewgetopt(1)program.# This program will...
以前我总想知道如何为我的Bash脚本创建命令行参数。经过搜索,我发现了2个函数可以处理这个问题,getopt函数和getopts函数。我无意争论哪一个函数更好的。getopts是一个shell内建命令,而且似乎比getopt更容易实现这个功能,所以在这篇文章里我准备讲讲getopts。
以前我总想知道如何为我的Bash脚本创建命令行参数。经过搜索,我发现了2个函数可以处理这个问题,getopt 函数和 getopts 函数。我无意争论哪一个函数更好的。getopts 是一个shell内建命令,而且似乎比 getopt 更容易实现这个功能,所以在这篇文章里我准备讲讲getopts。