简体   繁体   English

在bash中填充和读取数组

[英]Fill and read array in bash

What I actually do is getting a json string from the github api by calling 我实际上所做的是通过调用从github API获取json字符串

curl -u <userName> https://api.github.com/orgs/<orgName>/repos > test

I don't do it directly because in order to get jq to work, I've got to add {"result": at the beginning and } at the end of the file manually. 我不这样做,因为直接为了获得JQ的工作,我必须添加{"result":在开始和}在手动文件的末尾。

Now to my script in getthem 现在到我在getthem中的脚本

RESULT=$(cat test | jq ".result[] | .html_url");
COUNT=0
URLS=()

for line in $RESULT;
do
  echo $COUNT $line
  URLS[$COUNT]=$line
  COUNT=$(($COUNT+1))
done

#DEBUG
echo $URLS

COUNT=0
for url in $URLS;
do
  #DEBUG
  echo $COUNT $url

  #DO SOMETHING WITH RESULTS LATER
  # clone --bare $url $<onlyRepoName>.git

  COUNT=$(($COUNT+1))
done

My Problem: When I call bash ./getthem the first loop seems to work, but in the echos marked with #DEBUG there is only one line added to the array. 我的问题:当我调用bash ./getthem ,第一个循环似乎有效,但是在标有#DEBUG的回显中,仅向该行添加了一行。

Here the output I get 在这里我得到的输出

0 "https://github.com/<orgName>/<RepoName0>"
1 "https://github.com/<orgName>/<RepoName1>"
2 "https://github.com/<orgName>/<RepoName2>"
3 "https://github.com/<orgName>/<RepoName3>"
4 "https://github.com/<orgName>/<RepoName4>"
5 "https://github.com/<orgName>/<RepoName5>"
6 "https://github.com/<orgName>/<RepoName6>"
7 "https://github.com/<orgName>/<RepoName7>"

"https://github.com/<orgName>/<repoName0>" #this should be the whole array

0 "https://github.com/<orgName>/<repoName0>" #here again it should be all 8 entries...

What am I doing wrong? 我究竟做错了什么? Why aren't all 8 entries in the URLS array? 为什么URLS数组中没有全部8个条目?

To access all elements in the URLS array, you need to use ${URLS[@]} as below. 要访问URLS数组中的所有元素,您需要使用${URLS[@]} ,如下所示。

echo "${URLS[@]}"

COUNT=0
for url in "${URLS[@]}"
do
    # ...
done

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM