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
joaotellesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
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.
joaotellesAuthor Commented:
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
jmcgOwnerCommented:
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.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

joaotellesAuthor Commented:
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.
jmcgOwnerCommented:
You could try something like the following:

(echo AAAAA;echo BBBBB;echo CCCCC) | xargs -I'{}' echo hagrp -modify '{}' SystemList -delete xxxxxx

Open in new window


If you like the results, replace the first three echoes and their parentheses with your hagrp -list command and take out the last echo so the commands will be executed instead of just being printed out.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Instead of
a=AAAAAA
b=BBBBBB
c=CCCCCC
hagrp -modify ${a} SystemList -delete xxxxxx
hagrp -modify ${b} SystemList -delete xxxxxx

Open in new window

could you instead do
a=($(/opt/VRTS/bin/hagrp -list))
hagrp -modify ${a[0]} SystemList -delete xxxxxx
hagrp -modify ${a[1]} SystemList -delete xxxxxx

Open in new window

or
/opt/VRTS/bin/hagrp -list | xargs  -J %  hagrp -modify % SystemList -delete xxxxxx

Open in new window

TintinCommented:
or use a loop

#!/bin/bash
for value in $(/opt/VRTS/binhagrp -list)
do
  hagrp -modify $value SystemList -delete xxxx
done

Open in new window

joaotellesAuthor Commented:
Tks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.