shell scripting: calling subroutines

How do I define and call a subroutine in a ksh script (or any shell)? I'm writing a script that uses a lot of the same code over and over and I don't want to waste time/space.
Thanks,
Chuck

in bash shell it is something like this....

function function-name {
shell commands...
}

or just

function-name () {
shell commands...
}

#!/bin/bash

test ()
{
echo Hey I am here.
echo Now exiting function.
}

# Note: function must precede call.

# Now, call the function.

test

exit 0
------------------------------
here is another one which process passing arguments...

#!/bin/bash

func2() {
if [ -z $1 ]
# Checks if any params.
then
echo "No parameters passed to function."
return 0
else
echo "Param #1 is $1."
fi

if [ $2 ]
then
echo "Parameter #2 is $2."
fi
}

func2
# Called with no params
echo

func2 first
# Called with one param
echo

func2 first second
# Called with two params
echo

exit 0
----------------------

another important thing used in functions is return statement, which optionally takes an integer argument, which is returned to the calling script as the "exit status" of the function, and this exit status is assigned to the variable $?

I think korn shell also using same method. I am not sure.

[Edited by mib on 02-22-2001 at 02:24 AM]

Thank you very much :slight_smile: Worked like a charm!