Calling other file function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling other file function
# 1  
Old 08-17-2005
Calling other file function

Hi,

I am pretty new to unix.
Lets say i have a program(run_program) that will call another file function(functiona, in same directory):

hence, inside that run_program. i will just call "functiona xx xx" to refer and use that function. this run ok until i run this program from another folder.
it always say it cannot find the functiona?

Why this happened?
# 2  
Old 08-18-2005
Check your path variable by using echo $PATH . In this see if there is any instance of '.' (dot). If there is (and I think that there will be), then that is the source of your problem.

When the shell executes any command it looks for the full path of the command from the variable $PATH. Then the command is executed using the fork and exec mechanism.
Now I will use the example provided by you to explain:
Assume that you are in the directory /maldini/sh. The value of the '.' in the path is also /maldini/sh (pwd). Now when you run run_program, it runs with pwd as /maldini/sh. When the call to functiona is reached, the shell looks for the script in all the directories in the $PATH variable. Since functiona is also in /maldini/sh directory, the shell finds the full path of the script and can execute it. However if you run the command from /maldini, as sh/run_program, then when the call to functiona is reached, the shell again looks for the script in the $PATH. However, the '.' now refers to /maldini (as it is the pwd). So the shell cannot find the path to the functiona script and fails.

The solution to the problem is to use the full path of any of your own shell scripts that you may call in a script.
# 3  
Old 08-18-2005
MySQL

By default "." i.e current directory is in your path and you are able to access the functiona when it lies in same directory.

To make sure the function a is always call irrespective of location introduce a line in your shell script


. $YOU_PATH/functiona
Here onwards your call to functiona will always be successful irrespective of your present location.


Regards,
Rishi
# 4  
Old 08-19-2005
thanks for both of ur reply.
i solve the problem by cd to the working directory in my schedule job.
the reason why i never consider to use full path is because it will restrict my program flexibility or rather i need to change the path each time i need to use it in other place as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

3. Shell Programming and Scripting

Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing sub mergeDescription { system (q@awk -F'~' ' NR == FNR { A = $1 B = $2 C = $0 next } { n = split ( C, V, "~" ) if... (3 Replies)
Discussion started by: crypto87
3 Replies

4. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

5. Shell Programming and Scripting

Calling a function in cpp file inside shell script

Hi I need to call a function written in a cpp file with arguments inside the shell script..Can anyone help me how to do this:( (1 Reply)
Discussion started by: rkrish
1 Replies

6. Shell Programming and Scripting

Reading function calling statement from a file

Hi, I have a shell script 'sample.sh' which has some functions as below. #fun1 fun1() { date } #fun2() { echo hi } I want to run these functions as background processes and also redirect the output to a file. My function calling statements are in a different file 'sample.cfg' as... (3 Replies)
Discussion started by: rama_kodam
3 Replies

7. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

8. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

9. UNIX for Dummies Questions & Answers

Calling on function from file??

This may sounds dumb, but can I call on a function from a file? For example, I have a function file full of functions like below (no shell designation): func { echo "blah blah blah 1" } func2 { echo "blah blah blah 2" } func3 { echo "blah blah blah 3" } Am I able to call on any one... (3 Replies)
Discussion started by: douknownam
3 Replies

10. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies
Login or Register to Ask a Question