我需要从 poloniex rest 客户端下载图表数据,其中包含仅使用 bash 的多个选项。 我尝试了getopts,但实际上找不到使用具有多个参数的多个选项的方法。
这是我想要实现的目标
./getdata.sh -c currency1 currency2 ... -p period1 period2 ...
有我需要打电话给 wgetc x p
次的参数
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 %sn" "$arg1"
printf "Argument 2 is %sn" "$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
参考:BASH:getopts 从一个标志中检索多个变量
你可以调用./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