2

I tried to fetch all repos from a git user and put them into a array within a shell script. Somehow the array doesn´t recognize the new line as a separator and acts like there´s only one multi line element within the array.

Here´s my sample code:

someUser=Joe
declare -a repos=$(curl -s "https://api.github.com/users/$someUser/repos?page=$PAGE&per_page=100" | grep -e 'git_url*' | cut -d \" -f 4 | cut -d"/" -f5 | cut -d"." -f1)

for repo in $repos; do
  echo $repo
  // some more stuff
done

The output from the curl and cut looks like that:

RepoA
RepoB
RepoC
[...]

How do I get a new line element treated as a new element within the array? I use the array several times so I need one fixed container with all the repositories.

2 Answers 2

4

This is the correct way to iterate over the elements of a Bash array:

for repo in "${repos[@]}"; do

Also, to create an array with the output of a command, you need to wrap the $(...) subshell within (...), like this:

declare -a repos=($(curl -s ...))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the clarification but i still get a faulty output :( Still the first element in the array is the whole curl & cut output. edit
@Moka I noticed another problem in your script, fixed it now, see my updated post
Thanks @janos The second fix with the additional bracket solved my problem. I´ll answer this question with the complete code :)
1

Thanks to @janos i fixed the script. The additionsl bracket within the delcare line was the problem. Here is the full code. Maybe somebody would like to copy it.

#!/bin/bash

gitUrl="https://github.com"
gitUser="foobar"
cloneCmd="git clone"
fetchCmd="git fetch"

magenta="\033[35m"
green="\033[32m"
def="\033[0m"

declare -a repos=($(curl -s "https://api.github.com/users/$gitUser/repos?page=$PAGE&per_page=100" | grep -e 'git_url*' | cut -d \" -f 4 | cut -d"/" -f5 | cut -d"." -f1))

# Init clone
echo -e "$magenta Cloning new Repositories $def"
for repo in "${repos[@]}"
do
    if [ -d $repo ]; then
        echo -e "$green \tRepo $repo already exists $def"
        continue
    fi

    $cloneCmd $gitUrl/$gitUser/$repo

done
echo -e "$green Colning finished $def"

# Update Repos
echo -e "$magenta Updating Repositories $def"
for repo in "${repos[@]}"
do
    cd $repo
    $fetchCmd
    cd ..
done
echo -e "$green Update finished $def"

2 Comments

Do you seriously think anyone would want to use your broken script? 1. You're using a broken way to populate the array: use mapfile or read to properly populating it. 2. You're parsing json using chains of greps and cuts: broken and inefficient. Use a proper parser, e.g., jq. 3. You're putting code in variables. Code isn't data (this is not Lisp). Use a function instead. 4. Use more quotes. 5. Don't use hardcoded ANSI escape sequences. Use tput. I've probably missed a few antipatterns :).
Ok :D I'll try to fix those mistakes. I'm no professional but thanks for your ideas.

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.