问题描述
我有一个脚本可以在Ubuntu计算机上启动一些检查.
I have a script to launch some checks on Ubuntu machine.
我这样称呼我的脚本: ./script -f/tmp/file.txt --modules 001002003
I call my script like this : ./script -f /tmp/file.txt --modules 001 002 003
所有文件001、002等都是bash脚本.
All files 001, 002, etc... are bash scripts.
以下主要功能是我的问题:
The following main function is my problem :
main () {
OPTS=`getopt -o hf:m: --long help,file:,modules: -n 'script.sh' -- "$@"`
eval set -- "$OPTS"
[ $# -eq 0 ] && echo "Unknown options or parameters" && USAGE
while [ $# -gt 0 ]; do
case "$1" in
-f|--file)
FILE="$2"
shift
;;
-h|--help)
USAGE
exit 1
;;
-m|--modules)
MODULES="$2"
shift
;;
esac
shift
done
[ -z "$MODULES" ] && echo "No module specified" && USAGE
}
我希望$ MODULES变量包含例如 001002004
.
我尝试了轮换尝试其他方法,但这很复杂.
I would that $MODULES variable contains for example 001 002 004
.
I tried different things with shift, but that's some complicated..
理想情况下,如果我可以将"$ @"用作其余的脚本设置,那就太好了.
Ideally, if I can use "$@" as the rest of the script settings , this could be great.
例如,使用 ./script.sh -f/tmp/file.txt --modules"001 002 003"
,最后$ MODULES变量包含"001 002 003".
EDIT : With ./script.sh -f /tmp/file.txt --modules "001 002 003"
finally the $MODULES variable contains "001 002 003" , for example.
但是我有一个for循环,似乎没有在所有args上进行迭代,仅在第一个...上.每个 $ module
都包含"001 002 003".
But I have a for loop which seems to not iterate on all the args, only on the first... each $module
contains "001 002 003".
for module in "$MODULES"; do
echo "Importing Module $module"
. modules/$module >> "$FILE"
done
推荐答案
Getopt无法获取任意数量的值,但是您可以将模块列表作为参数usign进行传递,然后对其进行解析:
Getopt can't get an arbitrary numbers of values, but you can pass modules list as a parameter usign " and later parser it:
./script -f /tmp/file.txt --modules "001 002 003"
在脚本中,您可以获得每个单独的值:
Inside your script you can get each individual value:
for module in $MODULES ; do
echo $module
done
这篇关于Bash getopt-转移多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!