This will use a simple bash script that prints the scriptusagewhen the-hoption is passed, and it prints the content of the folder when the-poption is used with a specified path to the folder as the argument. The first:means thatgetoptswill not report any errors. Instead, we will handle...
运行示例:
第一个参数optstring是字符串,给出脚本所有的连词线参数。比如,某个脚本可以有三个配置项参数-l、-h、-a,其中只有-a可以带有参数值,而-l和-h是开关参数,那么getopts的第一个参数写成lha:,顺序不重要。注意,a后面有一个冒号,表示该参数带有参数值,getopts规定带有参数值的配置项参数,后面必须带有一个冒号(:)。
参考资料: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash https://www.computerhope.com/unix/bash/shift.htm
这里是一个例子,完成了上面usage函数的用法。我从http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/ 得到这段代码 代码如下: cmdline() { # got this idea from here: # http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getop...
这里是一个例子,完成了上面usage函数的用法。我从http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/ 得到这段代码 代码如下: cmdline() { # got this idea from here: # http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getop...
A common task in shell scripting is to parse command line arguments to your script. Bash provides the getopts built-in function to do just that. This tutorial explains how to use the getopts built-in function to parse arguments and options to a bash scri
$bashgetopts1.sh-m $bashgetopts1.sh-k Output: Example-2: Using option with a single argument This example shows the use of getopts command with an argument. Create a bash file named ‘getopts2.sh’ with the following code to test the code. Here, ‘:’ is used with ‘p’ to define...
这里是一个例子,完成了上面usage函数的用法。我从http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/得到这段代码 cmdline() { # got this idea from here: # http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu...
getopt()/getopts()是一个很好的选择。从这里被盗:The simple use of"getopt" is shown in this mini-script: 12345678910111213 #!/bin/bash echo"Before getopt" for i do echo $i done args=`getopt abc:d $*` set -- $args echo"After getopt" for i do echo"-->$i" done...