how to query an array to see if a value exists, and if it does do something











up vote
0
down vote

favorite












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
}









share|improve this question




















  • 2




    grep reads the input from stdin, maybe you wanted 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.
    – choroba
    Nov 12 at 16:22












  • Not sure what other information you would like. The other part of the script that checks the repo epoch is pretty much outputs the time in days to variables (foo, bar). But I will try out your idea
    – Jay Wisniewski
    Nov 12 at 16:56















up vote
0
down vote

favorite












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
}









share|improve this question




















  • 2




    grep reads the input from stdin, maybe you wanted 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.
    – choroba
    Nov 12 at 16:22












  • Not sure what other information you would like. The other part of the script that checks the repo epoch is pretty much outputs the time in days to variables (foo, bar). But I will try out your idea
    – Jay Wisniewski
    Nov 12 at 16:56













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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
}









share|improve this question















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
}






bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 19:50

























asked Nov 12 at 16:14









Jay Wisniewski

255




255








  • 2




    grep reads the input from stdin, maybe you wanted 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.
    – choroba
    Nov 12 at 16:22












  • Not sure what other information you would like. The other part of the script that checks the repo epoch is pretty much outputs the time in days to variables (foo, bar). But I will try out your idea
    – Jay Wisniewski
    Nov 12 at 16:56














  • 2




    grep reads the input from stdin, maybe you wanted 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.
    – choroba
    Nov 12 at 16:22












  • Not sure what other information you would like. The other part of the script that checks the repo epoch is pretty much outputs the time in days to variables (foo, bar). But I will try out your idea
    – Jay Wisniewski
    Nov 12 at 16:56








2




2




grep reads the input from stdin, maybe you wanted 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.
– choroba
Nov 12 at 16:22






grep reads the input from stdin, maybe you wanted 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.
– choroba
Nov 12 at 16:22














Not sure what other information you would like. The other part of the script that checks the repo epoch is pretty much outputs the time in days to variables (foo, bar). But I will try out your idea
– Jay Wisniewski
Nov 12 at 16:56




Not sure what other information you would like. The other part of the script that checks the repo epoch is pretty much outputs the time in days to variables (foo, bar). But I will try out your idea
– Jay Wisniewski
Nov 12 at 16:56












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Don't call out to grep. bash can tell you if an array key exists:



$ declare -A ary=([foo]=bar [baz]=qux)
$ [[ -v ary[foo] ]] && echo y || echo n
y
$ [[ -v ary[nope] ]] && echo y || echo n
n


See help test for the (brief) explanation of -v





I hacked this together, see if it helps:



yum -v repolist enabled | perl -MJSON -00 -lne '
if ($. == 1) {s/A.+?(^Repo-)/$1/ms}
s/nh+://sg;
%info = ();
for $line (split /n/) {
($key, $val) = split /s+:s+/, $line, 2;
$key =~ s/^Repo-//;
$info{$key} = $val;
}
push @repos, {%info} if $info{name};
}END{
print JSON->new->pretty->encode(@repos);
'


which outputs (on a machine near me)



[
{
"revision: 1530286202" : null,
"pkgs" : "6,710",
"name" : "CentOS-6 - Base",
"size" : "5.5 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock",
"excluded: 3" : null,
"baseurl" : "...",
"id" : "base",
"updated" : "Fri Jun 29 11:37:23 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:13 2018)"
},
{
"revision: 1541444458" : null,
"pkgs" : "222",
"name" : "CentOS-6 - Updates",
"size" : "2.6 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates&infra=stock",
"excluded: 3" : null,
"id" : "updates",
"baseurl" : "...",
"updated" : "Mon Nov 5 14:02:47 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "222",
"name" : "Zabbix Official Repository - x86_64",
"revision: 1542021892" : null,
"size" : "171 M",
"baseurl" : "...",
"updated" : "Mon Nov 12 06:24:54 2018",
"id" : "zabbix",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "15",
"name" : "Zabbix Official Repository non-supported - x86_64",
"id" : "zabbix-non-supported",
"updated" : "Sat Jun 14 22:53:53 2014",
"baseurl" : "...",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:15 2018)",
"size" : "1.0 M"
}
]





share|improve this answer























  • I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
    – Jay Wisniewski
    Nov 12 at 19:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266094%2fhow-to-query-an-array-to-see-if-a-value-exists-and-if-it-does-do-something%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Don't call out to grep. bash can tell you if an array key exists:



$ declare -A ary=([foo]=bar [baz]=qux)
$ [[ -v ary[foo] ]] && echo y || echo n
y
$ [[ -v ary[nope] ]] && echo y || echo n
n


See help test for the (brief) explanation of -v





I hacked this together, see if it helps:



yum -v repolist enabled | perl -MJSON -00 -lne '
if ($. == 1) {s/A.+?(^Repo-)/$1/ms}
s/nh+://sg;
%info = ();
for $line (split /n/) {
($key, $val) = split /s+:s+/, $line, 2;
$key =~ s/^Repo-//;
$info{$key} = $val;
}
push @repos, {%info} if $info{name};
}END{
print JSON->new->pretty->encode(@repos);
'


which outputs (on a machine near me)



[
{
"revision: 1530286202" : null,
"pkgs" : "6,710",
"name" : "CentOS-6 - Base",
"size" : "5.5 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock",
"excluded: 3" : null,
"baseurl" : "...",
"id" : "base",
"updated" : "Fri Jun 29 11:37:23 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:13 2018)"
},
{
"revision: 1541444458" : null,
"pkgs" : "222",
"name" : "CentOS-6 - Updates",
"size" : "2.6 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates&infra=stock",
"excluded: 3" : null,
"id" : "updates",
"baseurl" : "...",
"updated" : "Mon Nov 5 14:02:47 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "222",
"name" : "Zabbix Official Repository - x86_64",
"revision: 1542021892" : null,
"size" : "171 M",
"baseurl" : "...",
"updated" : "Mon Nov 12 06:24:54 2018",
"id" : "zabbix",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "15",
"name" : "Zabbix Official Repository non-supported - x86_64",
"id" : "zabbix-non-supported",
"updated" : "Sat Jun 14 22:53:53 2014",
"baseurl" : "...",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:15 2018)",
"size" : "1.0 M"
}
]





share|improve this answer























  • I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
    – Jay Wisniewski
    Nov 12 at 19:23















up vote
1
down vote













Don't call out to grep. bash can tell you if an array key exists:



$ declare -A ary=([foo]=bar [baz]=qux)
$ [[ -v ary[foo] ]] && echo y || echo n
y
$ [[ -v ary[nope] ]] && echo y || echo n
n


See help test for the (brief) explanation of -v





I hacked this together, see if it helps:



yum -v repolist enabled | perl -MJSON -00 -lne '
if ($. == 1) {s/A.+?(^Repo-)/$1/ms}
s/nh+://sg;
%info = ();
for $line (split /n/) {
($key, $val) = split /s+:s+/, $line, 2;
$key =~ s/^Repo-//;
$info{$key} = $val;
}
push @repos, {%info} if $info{name};
}END{
print JSON->new->pretty->encode(@repos);
'


which outputs (on a machine near me)



[
{
"revision: 1530286202" : null,
"pkgs" : "6,710",
"name" : "CentOS-6 - Base",
"size" : "5.5 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock",
"excluded: 3" : null,
"baseurl" : "...",
"id" : "base",
"updated" : "Fri Jun 29 11:37:23 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:13 2018)"
},
{
"revision: 1541444458" : null,
"pkgs" : "222",
"name" : "CentOS-6 - Updates",
"size" : "2.6 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates&infra=stock",
"excluded: 3" : null,
"id" : "updates",
"baseurl" : "...",
"updated" : "Mon Nov 5 14:02:47 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "222",
"name" : "Zabbix Official Repository - x86_64",
"revision: 1542021892" : null,
"size" : "171 M",
"baseurl" : "...",
"updated" : "Mon Nov 12 06:24:54 2018",
"id" : "zabbix",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "15",
"name" : "Zabbix Official Repository non-supported - x86_64",
"id" : "zabbix-non-supported",
"updated" : "Sat Jun 14 22:53:53 2014",
"baseurl" : "...",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:15 2018)",
"size" : "1.0 M"
}
]





share|improve this answer























  • I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
    – Jay Wisniewski
    Nov 12 at 19:23













up vote
1
down vote










up vote
1
down vote









Don't call out to grep. bash can tell you if an array key exists:



$ declare -A ary=([foo]=bar [baz]=qux)
$ [[ -v ary[foo] ]] && echo y || echo n
y
$ [[ -v ary[nope] ]] && echo y || echo n
n


See help test for the (brief) explanation of -v





I hacked this together, see if it helps:



yum -v repolist enabled | perl -MJSON -00 -lne '
if ($. == 1) {s/A.+?(^Repo-)/$1/ms}
s/nh+://sg;
%info = ();
for $line (split /n/) {
($key, $val) = split /s+:s+/, $line, 2;
$key =~ s/^Repo-//;
$info{$key} = $val;
}
push @repos, {%info} if $info{name};
}END{
print JSON->new->pretty->encode(@repos);
'


which outputs (on a machine near me)



[
{
"revision: 1530286202" : null,
"pkgs" : "6,710",
"name" : "CentOS-6 - Base",
"size" : "5.5 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock",
"excluded: 3" : null,
"baseurl" : "...",
"id" : "base",
"updated" : "Fri Jun 29 11:37:23 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:13 2018)"
},
{
"revision: 1541444458" : null,
"pkgs" : "222",
"name" : "CentOS-6 - Updates",
"size" : "2.6 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates&infra=stock",
"excluded: 3" : null,
"id" : "updates",
"baseurl" : "...",
"updated" : "Mon Nov 5 14:02:47 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "222",
"name" : "Zabbix Official Repository - x86_64",
"revision: 1542021892" : null,
"size" : "171 M",
"baseurl" : "...",
"updated" : "Mon Nov 12 06:24:54 2018",
"id" : "zabbix",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "15",
"name" : "Zabbix Official Repository non-supported - x86_64",
"id" : "zabbix-non-supported",
"updated" : "Sat Jun 14 22:53:53 2014",
"baseurl" : "...",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:15 2018)",
"size" : "1.0 M"
}
]





share|improve this answer














Don't call out to grep. bash can tell you if an array key exists:



$ declare -A ary=([foo]=bar [baz]=qux)
$ [[ -v ary[foo] ]] && echo y || echo n
y
$ [[ -v ary[nope] ]] && echo y || echo n
n


See help test for the (brief) explanation of -v





I hacked this together, see if it helps:



yum -v repolist enabled | perl -MJSON -00 -lne '
if ($. == 1) {s/A.+?(^Repo-)/$1/ms}
s/nh+://sg;
%info = ();
for $line (split /n/) {
($key, $val) = split /s+:s+/, $line, 2;
$key =~ s/^Repo-//;
$info{$key} = $val;
}
push @repos, {%info} if $info{name};
}END{
print JSON->new->pretty->encode(@repos);
'


which outputs (on a machine near me)



[
{
"revision: 1530286202" : null,
"pkgs" : "6,710",
"name" : "CentOS-6 - Base",
"size" : "5.5 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock",
"excluded: 3" : null,
"baseurl" : "...",
"id" : "base",
"updated" : "Fri Jun 29 11:37:23 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:13 2018)"
},
{
"revision: 1541444458" : null,
"pkgs" : "222",
"name" : "CentOS-6 - Updates",
"size" : "2.6 G",
"mirrors" : "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates&infra=stock",
"excluded: 3" : null,
"id" : "updates",
"baseurl" : "...",
"updated" : "Mon Nov 5 14:02:47 2018",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "222",
"name" : "Zabbix Official Repository - x86_64",
"revision: 1542021892" : null,
"size" : "171 M",
"baseurl" : "...",
"updated" : "Mon Nov 12 06:24:54 2018",
"id" : "zabbix",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:14 2018)"
},
{
"pkgs" : "15",
"name" : "Zabbix Official Repository non-supported - x86_64",
"id" : "zabbix-non-supported",
"updated" : "Sat Jun 14 22:53:53 2014",
"baseurl" : "...",
"expire" : "21,600 second(s) (last: Mon Nov 12 15:00:15 2018)",
"size" : "1.0 M"
}
]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 20:26

























answered Nov 12 at 18:14









glenn jackman

165k26142234




165k26142234












  • I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
    – Jay Wisniewski
    Nov 12 at 19:23


















  • I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
    – Jay Wisniewski
    Nov 12 at 19:23
















I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
– Jay Wisniewski
Nov 12 at 19:23




I know the array exists, my problem is, that there is more than one specific repo that I want to input to. So that's how I was specifying that the right repo information gets placed with the right repo_id. But my problem now is that, there is just mass adding of that field / info.
– Jay Wisniewski
Nov 12 at 19:23


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266094%2fhow-to-query-an-array-to-see-if-a-value-exists-and-if-it-does-do-something%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?