将位置参数转换为bash中的getopts的自定义集
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您致电:
You script should work if you call it with :
我完全错过了您将选项作为一个字符串传递的方式。
如果可以的话,请使用 @Philippe的答案。
如果您绝对必须将所有选项作为单个位置参数调用脚本,则可以执行此操作,但是您最好相信选项字符串的内容
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