Mike D
Mike D

Reputation: 6195

Collecting items into a shell array

I have a shell script that iterates over a series of values in an array and executes a command on the item.

For any command that returns an error code != 0, I would like to add that to an array of failures for later display.

Upvotes: 0

Views: 133

Answers (1)

Mike D
Mike D

Reputation: 6195

Since I figured this out while formulating the question, here is the code I used:

#!/bin/bash -u

array=( repo1 repo2 repo3 )
errorarray=()
for i in "${array[@]}"; do
    cd $i && git pull || errorarray+=($i)
done


echo errorarray has ${#errorarray[@]} items

for e in "${errorarray[@]}"; do
    echo "$e failed"
done;

(( ${#errorarray[@]} == 0 )); 
exit $?

This has an added benefit of returning a non-zero if any of the sub-commands failed.

Upvotes: 2

Related Questions