The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calling function inside awk jisha Shell Programming and Scripting 2 04-14-2008 07:44 AM
Calling a function ashika UNIX for Dummies Questions & Answers 5 09-12-2006 07:58 PM
calling c++ function from script Lebamb High Level Programming 3 06-24-2003 09:53 AM
c++ calling main() function norsk hedensk High Level Programming 3 01-22-2003 08:28 PM
calling a c function from shell seshagiri UNIX for Dummies Questions & Answers 4 12-06-2000 09:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-17-2005
maldini maldini is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 21
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 (permalink)  
Old 08-18-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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 (permalink)  
Old 08-18-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

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 (permalink)  
Old 08-19-2005
maldini maldini is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 21
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0