views:

104

answers:

1

I want to grep an environment variable

env | grep ABC_IJK[1,2]

currenlty defined variables are like this

ABC_IJK1=123
ABC_IJK1_XYZ=sn123:345
ABC_IJK2=999
ABC_IJK2_XYZ=sn321:999

I only want to get only this

ABC_IJK1=123
ABC_IJK2=999

get number of connections sum them(not 123 + 999) and save in N1 (this one has 2 connection)

so N1=2

get number of connections sum them and save in N2 for other machine (if this have four)

so N2=4

If the value is not there or the variable is not present then N1=1 for below:

ABC_IJK1=
ABC_IJK2=123

Then need to compare it with a MAX_CONNECTION variable

Thanks

A: 

N1 = $(grep -c '^ABC_IJK[12]=[0-9][0-9]*$')

will set the correct N1, I think: the [0-9] forces at least a digit, and the rest ensures only digits until the end. I am assuming that ABC_IJK1 and ABC_IJK2 are the only values you're interested in. grep -c counts the number of matches, which is all you want.

Henno Brandsma