0

the command ads2 cls create produces the following output:

cluster established ...
    nodes {
        node {
            name = "fdt-c-agx-0002";
            address = "http://172.16.11.89:9011/";
            state = "3";
        }
        node {
            name = "FDT-C-VM-0094";
            address = "http://172.16.11.49:9011/";
            state = "3";
        }
    }

What i'm trying to to is to find the name of each node and its status, assign each to a variable and print them so that the output looks like:

NODE-1 name has a state state #This conforms to the first node

NODE-2 name has a state state #This conforms to the second node

So I started with grep like status-nod1="$(ads2 cls create | grep state | cut -d '"' -f 2)" & name-nod1="$(ads2 cls create | grep name | cut -d '"' -f 2)".

This works, however finds all matches with state and 'name' and thus returns

3
3
fdt-c-agx-0002
FDT-C-VM-0094

So How can i output the match in the write order so that each node matches its state

1
  • You can pass multiple patterns to a single grep ex. grep -e name -e state or use regex alternation grep -E 'name|state' Mar 23, 2021 at 13:24

1 Answer 1

1

It's not the most effecient code, but it'll do the job.

#! /bin/bash

mapfile -t name < <(ads2 cls create | grep -Po "name = \"\K.*(?=\")")
mapfile -t state < <(ads2 cls create | grep -Po "state = \"\K.*(?=\")")

a=0
while [[ $a -gt -1 ]]
do
        echo "NODE-$(echo $a+1 | bc) ${name[$a]} has the state ${state[$a]}"
        a=$((a+1))
        if [[ -z ${name[$a]} ]]
        then
                a=-2
        fi
done

Paste that in a .sh file (e.g. node_status.sh), make it executable (sudo chmod a+x node_status.sh) and then run it (path/to/script/node_status.sh or ./node_status.sh if you're in the dir that the script is located in). The good thing about this script is that when you add node(s) (NODE-3 NODE-4 etc.), the script can handle that and will give you the name and status of every node (not just the first two). You can have 100 nodes, and it will give the name and status of all 100 nodes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .