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