如何查询数组以查看值是否存在以及是否执行某些操作

时间:2018-11-12 16:14:53

标签: bash

因此,我有脚本的这一部分,该脚本基于yum repolist all命令的输出来创建关联数组。该脚本逐行读取,然后将信息添加到数组中。但我还有更多信息想补充。我编写了一个脚本,比较了将回购从一种环境更新到另一种环境的时期。开发人员进行测试,测试人员进行生产。 我希望脚本的数组部分能够找到通常位于Repo-id字段中的存储库的名称,然后将脚本的第一部分中的信息提取为数组项。

这是我到目前为止所拥有的:

# 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"

我认为这将是我对数组的值搜索/输入,但似乎不起作用

if
  grep -q ius "${REPO_ARRAY[$i]}";
then
  REPO_ARRAY[repo_sync_dev\/test]=${foo}
  REPO_ARRAY[repo_sync_test\/prod]=${bar}
fi

我尝试将其添加到循环的结尾echo中。无济于事。甚至不确定我是否也做对了。

编辑:

因此,键/值似乎已添加到输出中。哪个好!但这增加了所有的肠胃。 w ^ 希奇不是很好。

{
  "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 
}

1 个答案:

答案 0 :(得分:1)

不要呼唤grep。 bash可以告诉您是否存在数组键:

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

有关help test的(简要)说明,请参见-v


我一起砍死了,看看是否有帮助:

yum -v repolist enabled | perl -MJSON -00 -lne '
    if ($. == 1) {s/\A.+?(^Repo-)/$1/ms}
    s/\n\h+://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);
'

输出(在我附近的机器上)

[
   {
      "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"
   }
]