[KLUG Members] calling functions from the shell

Peter Buxton members@kalamazoolinux.org
Mon, 9 Dec 2002 16:59:47 -0500


On Mon, Dec 09, 2002 at 03:29:01PM -0500, bill wrote:

> If I create the following file and mark it executable how would I call
> the function?

'. scriptname'

Don't bother making it executable, and lose the '#!/bin/bash' entry.

Seriously, bash has a simple include directive, 'source', AKA '.'. Any
POSIX shell fragment may be included by that command, and it will be
included and parsed/executed by the shell. (And by that, I mean that
your function, below, would not be *called*, but it would be parsed by
bash and CopyWinHost() will be a valid function from that point in the
including script on.)

Now, before you do this, READ 'man 1 bash' about functions and MEMORIZE
it before you shoot yourself in the foot with colliding variables. This 
is bash, not C++ or Ruby or Python.


> /bin/bash
> 
>          BACKDIR= /backup
>          WINCMD= /usr/bin/smbclient
> 
>          function CopyWinHost(){
> 
>          # tars and gzips "windows shares" to a local directory using samba's
>          # smbclient
>          # argument 1 is the remote host window's host name
>          # argument 2 is the share name to be backed up
> 
>             echo $1,$2,$3
>             REMOTE= $1
>             SHARE= $2
>             DEST= $3
> 
>           # create a tarred gzip file using samba to copy direct from a
>           # windows pc
>           # 12345 is a password.  Needs some password even if not defined on
>           # remote system
>             $WINCMD \\\\$REMOTE\\$SHARE 12345 -Tc -|gzip > $DEST
>             echo `date`": Done backing up "$REMOTE" to "$DEST
>             echo
>          }

-- 
for gpg key: http://killdevil.org/~peter