Operating System - HP-UX
1752782 Members
6453 Online
108789 Solutions
New Discussion юеВ

Re: Can ksh do filehandles?

 
Matt Hearn
Regular Advisor

Can ksh do filehandles?

I'm writing a script that will "for" loop its way through a file with a list of volume groups, grepping and awking as needed. I also need to be able to go through a long list of available disks and grab them one by one.

I can't figure out any way to open the disk file within an iteration of the for loop, and have the script remember where it was in the disklist when it left off. For example, if my vg file contains the following:

vg00
vg01

And my disk file contains:
/dev/dsk/c0t0d0
/dev/dsk/c0t0d1

I want to process vg00, which will open the disk file and allocate c0t0d0 to it. Then when it processes vg01 on the next pass through the for loop, I want it to know that the next available disk is c0t0d1, not start from the beginning.

I know I can do this in perl relatively easily, but I want to use HP-UX's native ksh for portability (this is to automate some things for DR, and I can't guarantee that the DR systems would have any kind of perl at all).

I considered the possibility of maintaining a separate temporary file of "used" disks and doing a diff on it and the main disk file to get sublist of available disks, but that's kinda kludgey. If I can open a filehandle to the disklist and then read each disk in turn, that would be extremely rad.

Thanks!!!
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: Can ksh do filehandles?

Hi Matt:

> I want to process vg00, which will open the disk file and allocate c0t0d0 to it. Then when it processes vg01 on the next pass through the for loop, I want it to know that the next available disk is c0t0d1, not start from the beginning.

Consider using a shell array with the disk devices and an index that keeps track of the last device processed. You could even add to the array as you go along if this is necessary.

> I know I can do this in perl relatively easily...

Of course. I'm glad you said that :-)

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Can ksh do filehandles?

Shalom,

Side note: You can guarantee perl and other resources are available in your DR servers by using Ignite tapes or network images to create said DR servers.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Can ksh do filehandles?

HI:

> SEP: Side note: You can guarantee perl and other resources are available in your DR servers by using Ignite tapes or network images to create said DR servers.

That presumes that the OP uses Ignite to clone to the DR site's bare metal. If that were the case you wouldn't expect the question posed with the disclaimer, "I can't guarantee that the DR systems would have any kind of perl at all)".

Regards!

...JRF...

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Can ksh do filehandles?

>Can ksh do filehandles?

Kind of but it may be more trouble than it is worth. See below.

In awk you can read from stdin or the list of files and you can also read from other files:
getline var < "FILE1"

In a real shell, you can use file numbers with exec and then use read with those numbers:
TMP=/var/tmp/ef.$$
cat < $TMP.1
line 1
line 2
line 3
line 4
EOF

cat < $TMP.2
line A
line B
line C
EOF

# open $TMP.2 as file 3
exec 3< $TMP.2
echo "exec status: $?"

while read LINE1; do
echo $LINE1
read -u3 LINE2
if [ $? -eq 0 ]; then
echo "$LINE2"
else
echo "EOF file #3"
fi
done < $TMP.1

# Close file
exec 3<&-

rm -f $TMP.1 $TMP.2