Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

asked on

Shell script - create variables from list output

Hi,

OS: RedHat5

I have a command that generates an output of a list: - can be multiples lines or only one...)

/opt/VRTS/bin/hagrp -list
AAAAAA
BBBBBB
CCCCCC
.
.

I have to assign each of these outputs into variables:
a=AAAAAA
b=BBBBBB

And so on until the last one...

Im not an expert in shell so the only way I know how to do it is using sed (that will run the command multiple times...)

So, is there a way that:

1- Create a loop that will through every line assigning it to variables (even if it 5 or 10 lines as output)? - running the main command only one time...
2- If no lines are available an error message is sent...

Tks,
Joao
Avatar of jmcg
jmcg
Flag of United States of America image

It would help to have a few more specifics. Like: how would the script know what variable names to use? How would the consumer of the variable know how many there are?

This looks like a situation where you would either want to put the lines into an array or process them line-by-line with a read block. Assigning them to variables just seems like a diversion along the way to solving the actual problem.
Avatar of joaotelles

ASKER

Hi,

Tks for the reply. Let me try to clarify...

The variables names to use doesnt matter that much.. can be a,b,c, and so on... depending on the number of lines the main commands outputs..

The script will run a command for each output line of the main command... so for eg.:

/opt/VRTS/bin/hagrp -list
AAAAAA
BBBBBB
CCCCCC
.

And the variables are assigned like this:
a=AAAAAA
b=BBBBBB
c=CCCCCC
.
.

PS: until the last one.. can go all the way to 10 itens listed for eg... but the script does not know the exactly number of lines here... so its necessary a loop...

Afterwards the script will use each of these variables, for eg, (another loop for to run this command for each variable):
hagrp -modify ${a} SystemList -delete xxxxxx
hagrp -modify ${b} SystemList -delete xxxxxx
.
.
until the last variable.

Tks,
Joao
I found this nice tutorial on using array variables in shell:

http://www.tutorialspoint.com/unix/unix-using-arrays.htm

I strongly suggest that you use the array approach instead of assigning individual variables.
Tks.

I can represent them as an array but how can I with the list command output (a list), refer to the second item of the list as a variable in the array?

My question may be very beginner but Im struggling to understand it.
ASKER CERTIFIED SOLUTION
Avatar of jmcg
jmcg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tks.