将位置参数转换为bash中的getopts的自定义集

发布于 2025-01-29 08:11:16 字数 1588 浏览 3 评论 0原文

我正在使用JAMF中的脚本,该脚本使用位置参数,但是我需要使用getopts来解析#4的各种参数。

位置1-3是静态的,并分别从JAMF以“/”(主机名和用户名)传递。在位置4中,我正在发送我需要在循环中使用的实际论点。我希望能够构建并添加“ opt”之后的“ $ {my_arr [@]}”:

OPTIND=4
while getopts "o:O:p:t:T:v:c:fsr?" opt
do
    case "${opt}" in
        o) osMinVers=${OPTARG}
            echoFunc "OPTARG: $OPTARG"
            ;;
        O) osMaxVers=${OPTARG};;
        p) appPath=${OPTARG};;
        t) jamfTrigger=${OPTARG};;
        T) patchName=${OPTARG};;
        v) appToUpdVers=${OPTARG};;
        c) verCheck=${OPTARG};;
        f) installIfMissing="true";;
        s) silent="true";;
        r) reboot="true";;
        ?) echo "Usage: script.sh -o -O -p -t -T -v [-f -s -r]";
           echo "      -o <osMinVers>";
           echo "      -O <osMaxVers>";
           echo "      -p <appPath>";
           echo "      -t <jamfPatchTrigger>";
           echo "      -T <patchName>";
           echo "      -v <appToUpdVers>";
           echo "      -c Greater then or less then";
           echo "         g/G = application must be greater than the version specified";
           echo "         l/L = application must be less than the version specified";
           echo "      -f Install if Missing";
           echo "      -s Silent";
           echo "      -r Reboot";
           exitFunc 90
    esac
done

运行此脚本的方式是:

sh /path/to/script.sh "/" "hostname" "username" "-o 18G1 -O 22Z9999 -p \"/Applications/Symantec Endpoint Protection.app\" -t SEPRemoval -T \"Symantec Removal\" -v 14.3.5055.3000"

I'm using a script in Jamf which uses positional arguments, but I need to use getopts to parse the various arguments only from #4.

Positions 1-3 are static and get passed from Jamf as "/", the host name and the user name, respectively. In position 4, I'm sending the actual arguments I need to use in my while loop. I want to be able to build and add "${my_arr[@]}" after "opt":

OPTIND=4
while getopts "o:O:p:t:T:v:c:fsr?" opt
do
    case "${opt}" in
        o) osMinVers=${OPTARG}
            echoFunc "OPTARG: $OPTARG"
            ;;
        O) osMaxVers=${OPTARG};;
        p) appPath=${OPTARG};;
        t) jamfTrigger=${OPTARG};;
        T) patchName=${OPTARG};;
        v) appToUpdVers=${OPTARG};;
        c) verCheck=${OPTARG};;
        f) installIfMissing="true";;
        s) silent="true";;
        r) reboot="true";;
        ?) echo "Usage: script.sh -o -O -p -t -T -v [-f -s -r]";
           echo "      -o <osMinVers>";
           echo "      -O <osMaxVers>";
           echo "      -p <appPath>";
           echo "      -t <jamfPatchTrigger>";
           echo "      -T <patchName>";
           echo "      -v <appToUpdVers>";
           echo "      -c Greater then or less then";
           echo "         g/G = application must be greater than the version specified";
           echo "         l/L = application must be less than the version specified";
           echo "      -f Install if Missing";
           echo "      -s Silent";
           echo "      -r Reboot";
           exitFunc 90
    esac
done

The way this script would be run is:

sh /path/to/script.sh "/" "hostname" "username" "-o 18G1 -O 22Z9999 -p \"/Applications/Symantec Endpoint Protection.app\" -t SEPRemoval -T \"Symantec Removal\" -v 14.3.5055.3000"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2025-02-05 08:11:16

如果您致电:

sh /path/to/script.sh "/" "hostname" "username" -o 18G1 -O 22Z9999 -p "/Applications/Symantec Endpoint Protection.app" -t SEPRemoval -T "Symantec Removal" -v 14.3.5055.3000

You script should work if you call it with :

sh /path/to/script.sh "/" "hostname" "username" -o 18G1 -O 22Z9999 -p "/Applications/Symantec Endpoint Protection.app" -t SEPRemoval -T "Symantec Removal" -v 14.3.5055.3000
烙印 2025-02-05 08:11:16

我完全错过了您将选项作为一个字符串传递的方式。

如果可以的话,请使用 @Philippe的答案。

如果您绝对必须将所有选项作为单个位置参数调用脚本,则可以执行此操作,但是您最好相信选项字符串的内容

path=$1
host=$2
user=$3
shift 3

eval set -- "$1"

while getopts "o:O:p:t:T:v:c:fsr?" opt
...

I totally missed the way you're passing the options as a single string.

If you can, use @Philippe's answer.

If you absolutely have to call the script with all the options as a single positional parameter, you can do this, but you'd better trust the contents of the option string

path=$1
host=$2
user=$3
shift 3

eval set -- "$1"

while getopts "o:O:p:t:T:v:c:fsr?" opt
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文