1

What I actually do is getting a json string from the github api by calling

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.

Now to my script in 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.

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?

3
  • ./getthem: Line 8: Syntaxerror at unexpected word »(« ./getthem: Line 8: URLS += ($line)' Commented Jun 12, 2017 at 8:14
  • Have it: without spaces! URLS+=($line) ... leads to the same output as in my question though Commented Jun 12, 2017 at 8:16
  • did it .. now my output is only the first 8 lines Commented Jun 12, 2017 at 8:19

1 Answer 1

2

To access all elements in the URLS array, you need to use ${URLS[@]} as below.

echo "${URLS[@]}"

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

Reference

Sign up to request clarification or add additional context in comments.

2 Comments

great this one did it! I guess it was something about the pointer vs value thing?
Multiple ways in which this answer can be improved. 1) useless use of cat 2) avoiding anti-pattern to dump file into variable (should be using input redirection on the file) 3) unquoted variable expansion (for line in $RESULT word-splitting could happen) 4) unquoted array expansion( for url in ${URLS[@]}, word-splitting again) 5) general practice to double-quote variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.