Calling functions in scripts directly

Hi,

I have a menu driven script that does various tasks, I want to be able to call functions directly from within other unix scripts from my menu script. Is this possible?

Its possible. Just make sure that the function is defined before its called.

How do you do it ranj? What do you mean about path names? Could you give me an example?

Thanks

Can you tell exactly what you are trying to achieve?

Bascially i have a menu:

doSQLSubMenu()
{
while true
do
showSQLSubMenu

 read answer

 MSG=

 case $answer in
    1\) ;;
    2\) ;;
    3\) ;;

    r|R\) break;;

    *\) badChoice;;
 esac

done
}

I want to make calls functions within different .sh scripts for option 1,2 and 3.

So say i had a function view() in a.sh

I would ideally want if you pressed 1 to call a.view()

Then another script perhaps b.sh

So if you pressed 2 it would call b.<function_name>

S.

There is a FPATH variable that should be set to point to the directory where the functions are defined. Check this link out - fpath

I'm not 100% sure i get this, is this saying create a libary of functions in another directory then ref this directory using the FPATH and just make calls to the required functions?

So if i have a file functions.sh that contains:

editSQLReleaseFile()
{
clear
echo "Editing..."
sleep 112
}

Then another file (say test.sh) with FPATH set to the location of the functions.sh file i can then make a call to editSQLReleaseFile from within test.sh?

i.e. in test.sh

echo "calling editSQLReleaseFile"
editSQLReleaseFile

?

It possible. What you have to do is to set the fpath, call autoload in the calling script and use the function. Like,

#!/bin/ksh
export FPATH=/dir/where/functions/are/in/scripts/ #better set this var in .profile
autoload #alias for typeset -fu; It loads the functions into memory
func1 #use the functions in the script. 

I am not near a system, so have not tested it. But this is possible. Guys, please correct if wrong.

Is it possible to just make a direct call to a function in another script without having to do this FPATH stuff? I may wish to reuse some scripts that have already been written.

Put your functions in a file, and source that file in any script that needs those functions:

## Functions file: $HOME/bin/funcs
xx()
{
  printf "%s\n" "$@"
}

xy()
{
  shift
  printf "%s\n" "$@"  
}
## Script that uses functions

. "$HOME/bin/funcs"

xx 12 34 56
xy qw er ty

FPATH is not standard (ksh only).

That is not exactly correct. The way this works changes depending on whether you are using ksh88 or ksh93. Suppose I write a script that contains a command like this:
xyzzy arg1 arg2 arg3
That "xyzzy" might be a command or it might be a function. So ksh88 will scan PATH looking for "xyzzy". If it finds it on PATH, it must be a command. If it is not found, then FPATH is scanned, if it is found there, it must be a function. Suppose I have both a command called "xyzzy" and a function called "xyzzy". I'm stuck...it will find the command first. This is where autoloading comes in. By autoloading "xyzzy", I declare that it must be a function.

With ksh93, autoloading is supported but deprecated. Instead you can put a directory both in FPATH and PATH. Such a directory is assumed to contain functions only. But you can sequence the search of commands and functions by the order of the directories in PATH.

This is weird enough that I suggest you forget about FPATH and just use cfajohnson's suggestion.

Nice thing to learn. Else I would have been stuck to the same old thought process!!! Thanks for the explanation.