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
Creating functions in shell script gjithin Shell Programming and Scripting 1 05-10-2008 03:15 AM
Calling a shell script from php eltinator Shell Programming and Scripting 1 04-22-2008 01:16 AM
Calling Shell Script onlyroshni Shell Programming and Scripting 1 10-22-2007 07:29 PM
Shell script functions r_subrahmanian Shell Programming and Scripting 5 12-13-2005 02:05 PM
Calling shell script ? Ravi_Kandula Shell Programming and Scripting 1 10-07-2002 09:30 AM

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 04-04-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Arrow Calling shell functions from another shell script

Hi,

I have a query ..
i have 2 scripts say 1.sh and 2.sh
1.sh contains many functions written using shell scripts.
2.sh is a script which needs to call the functions definded in 1.sh
function calls are with arguments.
Can some one tell me how to call the functions from 2.sh?

Thanks in advance.
JS
  #2 (permalink)  
Old 04-04-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Arrow

I have done a bit more research.
I know this is not possible especially with arguments. but still there should be a way out ..

JS
  #3 (permalink)  
Old 04-04-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
1.sh:
Code:
#!/bin/ksh
fun1() {
  typeset arg1="${1}"
  echo "in 1.sh::fun1 arg1->[${arg1}]"
}
2.sh:
Code:
#!/bin/ksh
echo "in 2.sh"
. 1.sh

fun1 "foo"
  #4 (permalink)  
Old 04-04-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Thank you vgersh99 !!
  #5 (permalink)  
Old 04-04-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
There is a better way of doing what you want to do using FPATH and/or autoload

For example, suppose you create the following directory within your home directory and put your function scripts in this directory. The filename should be the function name.

mkdir ~/.kshfunctions

In your .kshrc file, define the FPATH variable to point to that directory:

export FPATH=~/.kshfunctions

Functions will then be loaded on an as needed basis and results in faster script startup. To get a listing of which functions are actually currently loaded, just type 'functions' at the prompt. Use whence to find a function.

Code:
#!/usr/bin/ksh93
echo "in 2.sh"
fun1 "foo"
Code:
# fun1
function fun1
{
  print "In function fun1 arg1->[$1]"
}
Code:
$./2.sh
in 2.sh
In fun1 arg1->[foo]
  #6 (permalink)  
Old 04-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by fpmurphy View Post
There is a better way of doing what you want to do using FPATH and/or autoload

That is limited to the Korn shell and will not work in any other shell.
  #7 (permalink)  
Old 04-05-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Yes, I agree, it is limited to ksh93.
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 09:58 PM.


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