Operating System - HP-UX
1820360 Members
2802 Online
109623 Solutions
New Discussion юеВ

Re: function usage with commands scripts

 
SOLVED
Go to solution
Michael Tully
Honored Contributor

function usage with commands scripts

Hi,

I am in the midst of writing some scripts for a DR project. I have stacks of commands that I'll need to use. I want to build in some error checking using a function. That part is okay, (I think) but how do I overcome the usage of the function after each command line and use it like in a group of commands.

Here is part of the script and the function I'm using.

# Set up error function
set_err()
{
return=$?
if [ $return -eq 0 ]
then echo "Completed Task (rc=$return)\n"
else
echo "Error Encountered (rc=$return)\n"
rval=1 # There has been an error in the script
fi
}

Now I need to run several commands like this that do some LVM manoeuvring, vgexport, vgchgid, vgimports etc. I want to run a group of commands together without having to use the function for each and every line.

vgexport $VG1 || set_err >> $LOG
vgexport $VG2 || set_err >> $LOG
vgexport $VG3 || set_err >> $LOG

etc

vgimport ......
vgimport ......

Surely there is a better way. All suggestions are welcome, even perl ;^))
TIA
Michael
Anyone for a Mutiny ?
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor
Solution

Re: function usage with commands scripts

Michael,

Why didn't you consider putting all similar commands in a for loop?.

For ex.,

exporter()
{
for VG in VG1 VG2 VG3
do
vgexport $VG || set_err >> $LOG
done
}


If you don't like to hardcode VG's in 'for', you can read them from a configuration file so that you don't have to modify the script everytime.

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

Re: function usage with commands scripts

Based on Sri's idea, what about something like:

for VG in $(cat VGLIST)
do
vgexport $VG
done || set_err >> $LOG

I don't know if that would work, but a possibility for you?
Michael Tully
Honored Contributor

Re: function usage with commands scripts

Thanks guys... that's a nice start.... Those suggestions work well for dumping out the VG's to export and afew other bits.

Is there a better way to handle this particular part of the script when there are several of them (vgimports)?

vgimport -v -m $MAPDIR/server1.mapfile $VG1 `cat $BCVDISKDIR/file1_vg_bcv_dsk` || set_err >> $LOG
vgimport -v -m $MAPDIR/server1.mapfile $VG1 `cat $BCVDISKDIR/file2_vg_bcv_dsk` || set_err >> $LOG
etc
etc
Anyone for a Mutiny ?
Sridhar Bhaskarla
Honored Contributor

Re: function usage with commands scripts

Michael,


I think you can handle it better by creating standard files. For ex., for vg01, you would call the map file as vg01.map file, your bcv script as vg01_bcv_dsk etc., The only variable would be the volumegroup. That way you can very easily manipulate the script.

for VG in VG1 VG2 VG3
do
vgimport -v -m $MAPDIR/${VG}.map file -f `cat $BCVDISKDIR/${VG}_bcv_dsk` $VG || set_err >> $LOG
done

Bottom line is to maintain standard file names with only one variable which could be the VG itself.

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

Re: function usage with commands scripts

Michael..

Small correction, you do not need to cat the ${VG}_bcv_disk if you use -f option. So the command would be

vgimport -v -m $MAPDIR/${VG}.map file -f $BCVDISKDIR/${VG}_bcv_dsk $VG || set_err >> $LOG

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

Re: function usage with commands scripts

For the HP POSIX shell and K-shell (other POSIX shells like bash work the same way), a function is either contained within the script itself, or found in a directory specified by the FPATH (or ENV) variables. Typically, you put a standard set of functions in a special directory and set FPATH at the beginning of thew script.


Bill Hassell, sysadmin
Donny Jekels
Respected Contributor

Re: function usage with commands scripts

one question?

If you build this set_err function. The obviously you plan to use it all over your script! Is there a way you could populate the calling function to print its name in the set_err function output?

eg.

I have multiple functions in one script, but all the functions call this set_err function.

so can I do something like this.


if [[ $? -eq 0 ]]
then
printf " Calling (function) passed\n"
else
printf " calling (function) failed\n"
fi

If I can get this to work then I'll save a ton of lines in my future projects :-)
"Vision, is the art of seeing the invisible"