Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

define Variables from file content. 1

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
I have a file containing

ENC0
ENC1
....

how would I read the file contents and assign each line to a variable?

VAR1=ENC0
VAR2=ENC1
VAR3=.....

The lines (enclosure type) vary depending on the SAN type, we could have more than two lines.

I know I could use

awk 'NR==1'
awk 'NR==2'

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Here's one way:

[tt]((i=1))
for line in $(</path/to_file)
do
eval VAR$i=$line
(( i=i+1 ))
done[/tt]



HTH,

p5wizard
 
Thanks p5,

any other offers?

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Why not
Code:
set -A array $(< file)
then the lines will be ${array[0]}, ${array[1]}, etc.

However, see my comments on thread822-1451806

On the internet no one knows you're a dog

Columb Healy
 
You can use the positional parameters to hold the values:

[tt]set -- $(</path_to_file)

for i
do
echo var is $i
done[/tt]


HTH,

p5wizard
 
Arrays of course.

p5 good idea but I'm using this in a function and want to use the vars in other functions

so now I've got

Code:
get_enc() {

vxdmpadm listenclosure all | grep -w CONNECTED | grep -v DISKS|awk '(print $3)' > /tmp/list_enc.out

set -A array $(< /tmp/list_enc.out)
CAB1=${array[0]}
CAB2=${array[1]}
}

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
WARNING!!! WARNING!!! UUOG alert! Pedantic purist alert!

Joking apart, grep and awk as part of the same statement can always be simplified - try
Code:
vxdmpadm listenclosure all |awk '/CONNECTED/ && ! /DISKS/ {print $3}' > /tmp/list_enc.out

On the internet no one knows you're a dog

Columb Healy
 
Cheers for that, I guess I'm just lazy ;-)




Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top