是否可以将 getopts 与位置参数混合使用? [英] Is mixing getopts with positional parameters possible?

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

问题描述

我想设计一个 shell 脚本作为几个脚本的包装器.我想使用 getoptsmyshell.sh 指定参数,并将其余参数以相同的顺序传递给指定的脚本.

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

以上都应该可以调用为

test.sh param1 param2 param3

是否可以利用 myshell.sh 中的选项参数并将剩余的参数发布到底层脚本中?

Is it possible to utilize the options parameters in the myshell.sh and post remaining parameters to underlying script?

推荐答案

我想做一些类似于 OP 的事情,我找到了我需要的相关信息 这里这里

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.

希望有帮助!!

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

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