【问题标题】:Access List of Items from cURL Result从 cURL 结果访问项目列表
【发布时间】:2019-04-17 20:42:45
【问题描述】:

如果你去这个网址:http://artii.herokuapp.com/fonts_list,你会在列表中看到一个包含 417 个项目的列表。

我正在尝试创建一个变量来存储/访问这些列表。

我无法让它工作。

fontList=$(curl http://artii.herokuapp.com/fonts_list)
fontListCount=$(curl http://artii.herokuapp.com/fonts_list | wc -l)

for i in $(seq 1 $fontListCount);
do
    echo -e "I like this font --> " fontList[$i]
done

我一直在得到

I like this font -->  fontList[1]
...
I like this font -->  fontList[417]

关于如何做的任何提示?

【问题讨论】:

  • 当我访问该页面时,我会得到Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command heroku logs --tail

标签: arrays linux bash unix curl


【解决方案1】:

假设结果实际上是每行一个字体名称,您可以使用对readarray的单个调用:

readarray -t fontList < <(curl http://artii.herokuapp.com/fonts_list)

然后

for i in "${fontList[@]}"; do
  echo "I like this font --> $i"
done

【讨论】:

  • 你很好很好!第一次尝试的作品将接受这个答案。
【解决方案2】:

一般情况下,在扩展变量的值时,需要用${...}包围它们,否则bash会将变量名解释为普通字符串:

for i in $(seq 1 $fontListCount);
do
    echo -e "I like this font --> " ${fontList[$i]}
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多