Call a function in shell script from another shell script

I've 2 shell scripts viz., CmnFuncs.ksh and myScript.ksh.
1st script contains all common functions and its code is as below:

$vi CmnFuncs.ksh
#!/bin/ksh
RunDate()
{
 ....
 ....
 export Rundt=`date +%Y%m%d`
}

2nd script is invoking the above one and I expect to use the RunDt variable value in this 2nd script

$vi myScript.ksh
#!/bin/ksh
 ....
 ....
 . ./CmnFuncs.ksh
 echo $RunDt
 export myFile=`echo xyz$RunDt`
 echo $myFile

When I ran the 2nd script, output is as below

$ ./myScript.ksh
 
xyz
$
 

I was expecting as below

$ ./myScript.ksh
20140204
xyz20140204
$
 

Can you help me fix the issue? Appreciate it.

Thanks!

You never actually call the RunDT function, so the variable is never set.

Also, there is a typo, $Rundt vs $RunDt.

. ./myscript.sh

RunDate

echo $Rundt

Thanks so much, it worked.

I though calling the CmnFuncs.ksh script should do the trick, didn't know that I need to call the function seperately.

---------- Post updated at 05:17 PM ---------- Previous update was at 05:16 PM ----------

The mis-match in the variable name was a typo

In addition to what Corona688 said, you could also change:

 export myFile=`echo xyz$RunDt`

to:

 export myFile="xyz$Rundt"

You don't need command substitution here, just parameter expansion. (And, you don't need the export in this case unless you need to use the value stored in myFile in separate execution environments invoked by this script.)

Don - That is very valuable advise. Thanks!

Actually, when I implement the above logic in my original script (the one I gave above is sample), it is giving me the right output but also throwing below error

./wf_CITCOFxParm.ksh[12]: test: argument expected
./wf_CITCOFxParm.ksh[12]: test: argument expected

Any clue?

---------- Post updated at 05:30 PM ---------- Previous update was at 05:29 PM ----------

Line 12 is where I'm calling the CmnFuncs.ksh in the myScript.ksh

---------- Post updated at 05:30 PM ---------- Previous update was at 05:30 PM ----------

followed by on line 13, I have

RunDate()

Post the code as you have it right now. We can't see it from here.

make sure you quote your variables in the 'test' condition

if [ "${var}" operation "${var}" ]

To evaluate that error, I'd need to see at least the first 15 lines of wf_CITCOFxParm.ksh .

And, to run the function RunDate, you should use:

RunDate

not:

RunDate()

As vgersh99 suggested, placing double quotes around the variable in the if clause removed the errors.

Thank you all.

1 Like