Operating System - HP-UX
1834401 Members
2639 Online
110067 Solutions
New Discussion

Re: Advanced scripting challege

 
SOLVED
Go to solution
John J Read
Frequent Advisor

Advanced scripting challege

Hoping someone can help me with ideas or better yet, script syntax to the problem below.

I'd be extremely grateful and would be happy to share the full script if anyone is interested.

I have been working on a posix shell script that collects filesystem and volume group info on server which I can then use to duplicate the volume groups, LV's, filesystems structure on a new server. This will be used at my Disaster Recovery site to quickly build the external volume groups and filesystems after the Ignite Recovery.

I've build a script to provide the following output. The numbers represent number of disk devices in each VG.

vg10_disk_list 0 1 2 3
vg01_disk_list 4 5 6 7
vg46_disk_list 8 9 10 11 12

I initialized an array with each member being a disk device:

print "${dsks[0]}"
/dev/dsk/c1t7d3

print "${dsks[1]}"
/dev/dsk/c1t7d4

etc...


Now all I need to do, and the challenge I present to you is:

Provide syntax to change the following:

FROM:
vg10_disk_list 0 1 2 3

TO:
vg10_disk_list="${dsks[0]} ${dsks[1]} ${dsks[2]} ${dsks[3}"

Too many special characters for my scripting and pattern matching skills. I'm open to using another scripting language such as Perl if that suits the task better.

Let me know if you have any ideas. Currently, I just type in those array elements by hand. Otherwise the script is fully automated to capture and duplicate external disk configurations for any Unix server.

Thanks!

-john read







4 REPLIES 4
John J Read
Frequent Advisor

Re: Advanced scripting challege

There was a light typo:

The correct challenge is to change the following:

FROM:
vg10_disk_list 0 1 2 3

TO:
vg10_disk_list="${dsks[0]} ${dsks[1]} ${dsks[2]} ${dsks[3]}"

thanks again.
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Advanced scripting challege

Hi,

You can manipulate STR variable here and generate the output for as many variables as you need.


STR="vg10_disk_list 0 1 2 3"
VG=$(echo $STR|awk '{print $1}')
printf "$VG=\""
for I in $(echo $STR |sed 's/'$VG'//g')
do
printf "\$\{dsks\[$I\]\}"
done
printf "\"\n"

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Chris Vail
Honored Contributor

Re: Advanced scripting challege

Try some variation of this:
#/bin/ksh
COUNTER1=1
for DEV in `lvdisplay -v /dev/YOURVG/YOURLVOL|grep dsk|awk '{ print $2 }'|uniq`
do
DSK[$COUNTER1]=$DEV
(( COUNTER1 = "$COUNTER1" +"1" ))
done

This will pass the /dev/dsk/cXtXdX info to the subscripted variable DSK. The variable will count from 1 up instead of 0 up.


Chris
John J Read
Frequent Advisor

Re: Advanced scripting challege


Thanks! Here's how the final version ended up.


Source file:
#cat $vg_diskfile
vg07_disk_list 0 1 2 3 4 5 6 7 8 9 10 11 12 13
vg28_disk_list 14
vg11_disk_list 15 16 17 18


Script to make changes:
#!/bin/sh

vg_diskfile=/home/n721jjr/DR/vg_diskfile

LINES=`wc -l $vg_diskfile |awk '{print $1}'`
COUNT=1

while [[ $LINES -ge $COUNT ]]
do

STR=`sed -n ${COUNT}p $vg_diskfile`
VG=$(echo $STR|awk '{print $1}')
printf "$VG=\""
for I in $(echo $STR |sed 's/'$VG'//g')
do
printf "\$\{dsks\[$I\]\} "
done
printf "\"\n"
COUNT=$(($COUNT+1))

done



output:
vg07_disk_list="${dsks[0]} ${dsks[1]} ${dsks[2]} ${dsks[3]} ${dsks[4]} ${dsks[5]} ${dsks[6]} ${dsks[7]} ${dsks[8]} ${dsks[9]} ${dsks[10]} ${dsks[11]} ${dsks[12]} ${dsks[13]} "
vg28_disk_list="${dsks[14]} "
vg11_disk_list="${dsks[15]} ${dsks[16]} ${dsks[17]} ${dsks[18]} "


ps. I also liked the second response!


Thanks again.