Shell脚本:是混合与位置参数可能getopts的? [英] Shell Script : is mixing getopts with positional parameters possible?

查看:259
本文介绍了Shell脚本:是混合与位置参数可能getopts的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个外壳脚本的一对夫妇脚本的包装。我想指定 myshell.sh 使用 getopts的参数,并以相同的顺序将余下的参数给脚本指定。

I want to design a shell script as a wrapper for a couple of scripts. I would like to specify parameters for myshell.sh using getopts and pass the remaining parameters in the same order to the script specified.

如果 myshell.sh 就像执行:

myshell.sh -h hostname -s test.sh -d waittime param1 param2 param3

myshell.sh param1 param2 -h hostname param3 -d waittime -s test.sh

myshell.sh param1 -h hostname -d waittime -s test.sh param2 param3

所有上述应能够如

All of the above should be able to call as

test.sh param1 param2 param3

是否有可能利用在 myshell.sh 和后剩余的参数基本的脚本?

推荐答案

对不起,评论一个古老的线程,但想到我会张贴那些像我谁正在寻找如何做到这一点...

Sorry for commenting on an old thread, but thought I'd post for those, like me who were searching on how to do this...

我想要做类似的东西OP,我发现我所需要的相关信息<一个href=\"https://web.archive.org/web/20130927000512/http://www.devhands.com/2010/01/handling-positional-and-non-positional-command-line-arguments-from-a-shell-script/\">here和这里

I wanted to do something similar to the OP, and I found the relevant information I required here and here

从本质上讲,如果你想要做的事,如:

Essentially if you want to do something like:

script.sh [options] ARG1 ARG2

然后得到您的选择是这样的:

Then get your options like this:

while getopts "h:u:p:d:" flag; do
case "$flag" in
    h) HOSTNAME=$OPTARG;;
    u) USERNAME=$OPTARG;;
    p) PASSWORD=$OPTARG;;
    d) DATABASE=$OPTARG;;
esac
done

然后你就可以得到你的位置参数是这样的:

And then you can get your positional arguments like this:

ARG1=${@:$OPTIND:1}
ARG2=${@:$OPTIND+1:1}

更多信息和细节都可以通过上面的链接。

More information and details are available through the link above.

希望帮助!

这篇关于Shell脚本:是混合与位置参数可能getopts的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆