Linux 如何使用多个选项的多个参数处理bash
我只需要使用bash从poloniex rest客户端下载图表数据,并提供多个选项。 我尝试了getopts,但找不到一种使用多参数的多选项的方法 这就是我想要实现的目标Linux 如何使用多个选项的多个参数处理bash,linux,bash,macos,poloniex,Linux,Bash,Macos,Poloniex,我只需要使用bash从poloniex rest客户端下载图表数据,并提供多个选项。 我尝试了getopts,但找不到一种使用多参数的多选项的方法 这就是我想要实现的目标 ./getdata.sh -c currency1 currency2 ... -p period1 period2 ... 具有调用wget所需的参数cxp次 for currency in c for period in p wget https://poloniex.com/public?com
./getdata.sh -c currency1 currency2 ... -p period1 period2 ...
具有调用wget所需的参数cxp
次
for currency in c
for period in p
wget https://poloniex.com/public?command=returnChartData¤cyPair=BTC_{$currency}&start=1405699200&end=9999999999&period={$period}
嗯,我正在明确地写下我的最终目标,就像现在很多其他人在寻找它一样。像这样的东西对你有用吗
#!/bin/bash
while getopts ":a:p:" opt; do
case $opt in
a) arg1="$OPTARG"
;;
p) arg2="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
printf "Argument 1 is %s\n" "$arg1"
printf "Argument 2 is %s\n" "$arg2"
然后可以如下方式调用脚本:
./script.sh -p 'world' -a 'hello'
上述各项的输出将为:
Argument 1 is hello
Argument 2 is world
更新
您可以多次使用同一选项。解析参数值时,可以将它们添加到数组中
#!/bin/bash
while getopts "c:" opt; do
case $opt in
c) currs+=("$OPTARG");;
#...
esac
done
shift $((OPTIND -1))
for cur in "${currs[@]}"; do
echo "$cur"
done
然后可以按如下方式调用脚本:
./script.sh -c USD -c CAD
输出将是:
USD
CAD
参考资料:您可以拨打
/getdata.sh“currency1 currency2”“period1 period2”
getdata.sh
内容:
c=$1
p=$2
for currency in $c ; do
for period in $p ; do
wget ...$currency...$period...
done
done
不幸的是,我需要类似于./script.sh-p“world”叔叔-a“how”“you”的内容@hevi我更新了我的答案,并提供了一个示例,演示了如何多次使用同一选项。也许这就是处理多个值所需要做的。这不完全是我想要的,但很简单很好的解决方案,谢谢。