The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help on function call kamel.seg Shell Programming and Scripting 3 01-08-2008 04:16 AM
function call kamel.seg Shell Programming and Scripting 2 12-28-2007 11:58 AM
call function Jamil Qadir Shell Programming and Scripting 4 03-20-2007 02:07 AM
Help with a function call Stevhp High Level Programming 6 03-04-2007 11:44 PM
function call forever_49ers Shell Programming and Scripting 3 09-13-2006 03:26 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-20-2007
Registered User
 

Join Date: Feb 2007
Location: Chennai
Posts: 8
How to Call a Function with in two files

Hi all,

I am having a problem with my code

I am having a file from which i am calling a function, which is present in another file, for this i am using . <file name> followed by the function name, when i am doing like this, second file completely it is executing...

example

File1 Code:

. file2.ksh
tmp=1
fn_test $tmp

File2 Code:

fn_test()
{
echo $tmp
}
echo test

here the output will be

1
test

where i want only the value present in that function, i don;t want the code present outside of that function to execute

how to call only the particular function present in the file 2

thanks in advance
Reply With Quote
Forum Sponsor
  #2  
Old 02-20-2007
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 791
afaik, when using ksh, you may not call only a function which is present in another file. you may execute another file (execute it completely)

use Perl, Python, TCL to do what you are trying
Reply With Quote
  #3  
Old 02-20-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
. file2.ksh > /dev/null
tmp=1
fn_test $tmp
Reply With Quote
  #4  
Old 02-20-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Quote:
Originally Posted by anbu23
Code:
. file2.ksh > /dev/null
tmp=1
fn_test $tmp
Doesn't suppress execution of commands outside function definitions, this can be very dangerous.


Jean-Pierre.
Reply With Quote
  #5  
Old 02-20-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,693
Quote:
Originally Posted by hsekol
example

File1 Code:

. file2.ksh
tmp=1
fn_test $tmp

File2 Code:

fn_test()
{
echo $tmp
}
echo test

here the output will be

1
test

where i want only the value present in that function, i don;t want the code present outside of that function to execute
If you can make File2 to contain functions and only functions, then the code in File1 would be fine.
Something like
Code:
    fn_test()
    {
          echo $tmp
    }
    fn_echo ()
    {
          echo test
    }
Reply With Quote
  #6  
Old 02-20-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
Vino has it. Create an "include" file. Source the file which has just functions.
So if you have 3 files: file1.sh file2.sh and myfunctions.sh:
The first executable line in file1.sh is:
Code:
. /path/to/myfunctions.sh
The first executable line in file2.sh is:
Code:
. /path/to/myfunctions.sh
Note the leading dot followed by a space.
Reply With Quote
  #7  
Old 02-20-2007
Registered User
 

Join Date: Feb 2007
Location: Chennai
Posts: 8
hsekol

Hi all,

Thanks for your reply,

I tried the following code and it is working for me

. file2.ksh > /dev/null

but aigles has posted that "Doesn't suppress execution of commands outside function definitions, this can be very dangerous." can any body explain in what way it is dangerous for me to use this method

Actually my requirement is i have a file, which i need to run the both from command line and also from any file, for which i have created one main script and i have written some code outside of the function and calling the main function to execute from command line

can i proceed using the above code are is there any approach or procees to acheive it, if so please reply me

Thanks in advance
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0