- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: function usage with commands scripts
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 03:27 PM
тАО02-17-2003 03:27 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 03:39 PM
тАО02-17-2003 03:39 PM
SolutionWhy 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 03:43 PM
тАО02-17-2003 03:43 PM
Re: function usage with commands scripts
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 08:09 PM
тАО02-17-2003 08:09 PM
Re: function usage with commands scripts
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 08:27 PM
тАО02-17-2003 08:27 PM
Re: function usage with commands scripts
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 08:34 PM
тАО02-17-2003 08:34 PM
Re: function usage with commands scripts
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-17-2003 09:51 PM
тАО02-17-2003 09:51 PM
Re: function usage with commands scripts
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2003 07:05 AM
тАО04-19-2003 07:05 AM
Re: function usage with commands scripts
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 :-)