1835781 Members
3809 Online
110085 Solutions
New Discussion

Shell Script : Wrapper

 
SOLVED
Go to solution
Sudip Hore_1
Occasional Contributor

Shell Script : Wrapper

Can somebody give me some example of a wrapper script.
5 REPLIES 5
Anthony Goonetilleke
Esteemed Contributor
Solution

Re: Shell Script : Wrapper

Depends what you are trying to do...
For example a really simple ftp wrapper script could get the username and time of everyone using the ftp command.

#!/bin/sh
LOGFILE=/var/adm/ftp/ftp.log
echo "`whoami` on `uname -n` opts : $@ nStart ftp : `date`">> $LOGFILE
/usr/bin/ftp $@
echo "End ftp : `date`">> $LOGFILE
echo "n">> $LOGFILE

Minimum effort maximum output!
Stefan Farrelly
Honored Contributor

Re: Shell Script : Wrapper


A wrapper script is one which is written to call another script/program, but the wrapper now allows you to control the environment by setting things like environment variables, paths, output, errors etc.

eg. if you have a simple script called say daily_backup then to put a wrapper around it you could write another script which then calls daily_backup, eg;

#!/bin/sh
ERRORLOG=/tmp/daily_backup.error
LOG=/tmp/daily_backup.log

daily_backup > $LOG 2> $ERRORLOG

RETURN_CODE=$?
if [ $RETURN_CODE -ne 0 ]
then
echo "daily_backup failed, return code=$RETURN_CODE" >>$ERRORLOG
fi
Im from Palmerston North, New Zealand, but somehow ended up in London...
Bill Hassell
Honored Contributor

Re: Shell Script : Wrapper

For most servers, the find command is quite adverse to the health of your system. Many new users were told by a teacher:

"To find a file, use find / ..."

Unfortunately, this could take hours on a 500 Gbyte system, affecting every user on the system by issuing directory searches on every mounted disk. If there are NFS mounted disks the task also hits the network very hard.

I put a wrapper on find to prevent this newbie-type error:

--

#!/usr/bin/sh
#
# Replace find with a wrapper
#
if [ $1 = "/" ]
then
echo
echo "DO NOT USE: find /"
echo "It will affect every user on the system."
echo "Please search specific directories"
exit 1
fi
exec /usr/bin/find.real "$@"

--

Create this script with the name: find

Then rename /usr/bin/find to /usr/bin/find.real and move the script called find to /usr/bin. Make sure it is executable (chmod 755 /usr/bin/find).


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Shell Script : Wrapper

Hi:

Certainly the above posts provide very good examples of wrappers.

Consider too, that another use of wrappers is to be able to call a "super" script (or other piece of code) which requires a large number of arguments with a minimum of arguments. This is a user convenience. For instance, rather than, having to pass the filesystem type as on argument to a hypothetical super script, several wrapper scripts are created -- one for hfs, one for vxfs, etc. The wrappers are named to denote their intended filesystem targets.

...JRF...
Tom Danzig
Honored Contributor

Re: Shell Script : Wrapper

Keep in mind that you replace and HP executables with wrapper script as shown above, the wrapper will be deleted with the HP executable if you patch the command you "wrapped".