Korn shell scripting

April 12, 2010

I was wondering how to call functions from one shell script to another. I played a bit and produced this example which shows how to do that:

First file is myfile.sh

  1. #!/bin/ksh
  2. myVar=myvariableValue;
  3. print $myVar;
  4. myFoo(){
  5. print “function myFoo was called”;
  6. }
  7. myFoo;
  8. . ./myFile2.sh
  9. myFoo2;
  10. print “print variable from myFoo2 \’$myVar2\'”;

On line 13 we are calling function defined in another file, and on line 14 using variable defined in another file.

Line 11 does the whole trick. Here is another file myFile2.sh

  1. #!/bin/ksh
  2. print “myFile2 was called”
  3. myVar2=”variable in myFile 2″;
  4. myFoo2(){
  5. print “function myFoo2 was called”
  6. }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: