So I have this part of a script that creates associative arrays based on output from a yum repolist all command. The script reads line by line and then adds the information into the array. But I have more information that I would like to be added as well. I wrote a script that compares the epoch of when repos are updated from one enviroment to another. dev to test, test to prod.
What I would like, is for the array portion of the script to be able to find the name of the repo, typically held in Repo-id field, and then pull the information in the first part of the script to be input as array items.
Here is what I have so far:
# first sed replaces any blank lines with --
json=$(yum -v repolist all | grep -B2 -A6 "enabled" | sed 's/^$/--/')
# declare a new array to keep things in
declare -A REPO_ARRAY
# read lines and add them to the array if they match
while read line
do
case "${line}" in
*Repo-name* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_name]="${line}"
;;
*Repo-id* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_id]="${line}"
;;
*Repo-size* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_size]="${line}"
;;
*Repo-updated* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_updated]="${line}"
;;
*Repo-pkgs* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_pkgs]="${line}"
;;
*Repo-expire* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_expire]="${line}"
;;
*Repo-revision* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_revision]="${line}"
;;
*Repo-baseurl* )
line=$(echo "${line}" | sed -e 's/^[^:]*://' | sed -e 's,^ *,,')
REPO_ARRAY[repo_url]="${line}"
;;
# if we see -- that means the end of the repo
-- )
for i in "${!REPO_ARRAY[@]}"
if
grep -q ius <<< "${REPO_ARRAY[$i]}"
then
REPO_ARRAY[repo_sync_dev\/test]=${iustest_diff}
REPO_ARRAY[repo_sync_test\/prod]=${iusprod_diff}
fi
for i in "${!REPO_ARRAY[@]}"
do
echo "$i"
echo "${REPO_ARRAY[$i]}"
done | jq -n -R 'reduce inputs as $i ({}; . + { ($i): (input|(tonumber? // .)) })'
esac
done <<< "$json"
And I am thinking this would be my value search/input to array but it doesn't seem to be working
if
grep -q ius "${REPO_ARRAY[$i]}";
then
REPO_ARRAY[repo_sync_dev\/test]=${foo}
REPO_ARRAY[repo_sync_test\/prod]=${bar}
fi
I've tried adding it to the ending echo for loop. To no avail. Not even sure if I am doing this right either.
EDIT:
So it looks like the key's/values are getting added to the output. WHICH IS GOOD! But it's adding to all the enteries. W hich isn't great.
{
"repo_expire": "21,600 second(s) (last: Mon Nov 12 05:44:16 2018)",
"repo_url": "http://...",
"repo_id": "base",
"repo_pkgs": "6,713",
"repo_sync_dev/test": 6,
"repo_revision": 1530286202,
"repo_name": "CentOS-6 - Base",
"repo_size": "5.5 G",
"repo_updated": "Fri Jun 29 08:37:23 2018",
"repo_sync_test/prod": 851
}
grep -q ius <<< "${REPO_ARRAY[$i]}"? Most of the code you supplied in the question is only vaguely relevant to the answer, so hard to guess.