Calling functions in scripts directly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling functions in scripts directly
# 8  
Old 04-23-2007
Yes..its possible

It possible. What you have to do is to set the fpath, call autoload in the calling script and use the function. Like,
Code:
#!/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.
# 9  
Old 04-23-2007
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.
# 10  
Old 04-23-2007
Quote:
Originally Posted by LiquidChild
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?

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

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

xy()
{
  shift
  printf "%s\n" "$@"  
}

Code:
## Script that uses functions

. "$HOME/bin/funcs"

xx 12 34 56
xy qw er ty


# 11  
Old 04-23-2007
Quote:
Originally Posted by ranj@chn
There is a FPATH variable that should be set to point to the directory where the functions are defined. Check this link out - fpath


FPATH is not standard (ksh only).

# 12  
Old 04-23-2007
Quote:
Originally Posted by ranj@chn
It possible. What you have to do is to set the fpath, call autoload in the calling script and use the function. Like,
Code:
#!/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.
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.
# 13  
Old 04-27-2007
Bug Thanks

Nice thing to learn. Else I would have been stuck to the same old thought process!!! Thanks for the explanation.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash calling a few functions syntax error

In the bash function below if the user selets "y" then the menu function is called and if they select "n" the move function is called. That all seems to work, my question is after the files are moved an echo, line in bold is displayed and another function called backup is called. I am getting a... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Call functions from other scripts

i have a file that contains functions and i want the functions to be available in another script. of course, the ideal situation here would be to put the functions in the same script, but i dont own these scripts. so I have to call the functions file from a different script. how do i... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Calling Bash Functions in the BG

I'm trying to call some functions in the background so that I can multitask in this script. Not working so hot. The functions I call don't ever seem to get called. I'm doing it the exact same way in another script and it's working like a champ so I'm very confused. Here's a pretty simple repro: ... (7 Replies)
Discussion started by: stonkers
7 Replies

4. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

5. AIX

Calling functions from main program from dlopened library function

Hello All, I am trying to call a function from the calling main program from a dlopened library function, below is the entire code, when I execute it it crashes with sigill. Can you guys help me out I guess I am missing out on the linker flag or something here. besides I am new to AIX and... (1 Reply)
Discussion started by: syedtoah
1 Replies

6. Shell Programming and Scripting

Calling multiple functions in parallel

Hello, I have multiple functions within a shell script. eg. function_database_backup, unix_tar_creation, etc. I would like to run these functions in parallel, as each is independent of the other. If these were simple commands, I could have probably run each of the commands in background. ... (1 Reply)
Discussion started by: neil.k
1 Replies

7. Shell Programming and Scripting

Is it possible make the shell read functions 1 by 1 and calling an other function?

Greetings, I m wondering if it's possible do do the following : I have a simple function called "FindMoveDelete" which does the following : FindMoveDelete() { find . -iname "$FILENAME*.ext" -exec mv {} "$PATH/$VAR" \; && find . -maxdepth 1 -type d -iname "$FILENAME*" -exec rm -rf {}... (6 Replies)
Discussion started by: Sekullos
6 Replies

8. Shell Programming and Scripting

Calling Functions of Other K Shell Program

Hi, I have a K shell a.ksh function abc { // Some logic } In b.ksh i have included the a.ksh ./a.ksh I want to call the abc function from this b.ksh script. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

9. Shell Programming and Scripting

Check if script run by a user directly or via other scripts

Hi, i have a script 'a.sh' that should be called only by certain scripts like b.sh, c.sh Inside a.sh, how can i determine 1) if this script was run directly from command prompt (or scheduler) 2) if called via other scripts? Is there an easy way to get parent process name (not just pid),... (2 Replies)
Discussion started by: ysrinu
2 Replies

10. Shell Programming and Scripting

How to call C functions in shell scripts?

How can i call dynamic C functions in shell scripts? (5 Replies)
Discussion started by: agarwal
5 Replies
Login or Register to Ask a Question