Passing variable from called script to the caller script

Hi all,

Warm regards!

I am in a difficult situation here. I have been trying to create a shell script which calls another shell script inside. Here is a simplified version of the same.

Calling Script.

#!/bin/ksh

# want to run as a different process
/path_to_called_script/called_script.ksh 10 20

#some other operations

called script

#!/bin/ksh

sqlplus -s ${connection} <<EOF

sample_procedure($1,$2);
exit;
EOF

problem is $1 and $2 is coming as empty . it contains nothing. What's the correct procedure to pass variable into sqlplus from shell script? please help.

let me know in case additional info is needed.

Shell in use -ksh

If I replace 'sqlplus' with 'cat' it obviously works:

$ cat caller.ksh

#!/bin/ksh

# want to run as a different process
./called_script.ksh 10 20

$ cat called_script.ksh
#!/bin/ksh

cat -s ${connection} <<EOF

sample_procedure($1,$2);
exit;
EOF

$ ./caller.ksh

sample_procedure(10,20);
exit;

$

So either these aren't your actual scripts, or the error lies elsewhere.

Remember that set -- ... can alter the values of $1 $2 ...

I am calling the script using the notation /path/called_script.sh

and not ./called_script.sh

do you think it's making a difference?

No, it does not.

Either these aren't your actual scripts, or the error lies elsewhere.

This is being called like this

${MMHOME}/external/scripts/belk_nil_prc_thread.ksh 10 20

##Inside belk_nil_prc_thread.ksh

function EXEC_BELK_NEW_ITEM_LOC_PRICING
{
   echo "set feedback off
      set heading off
	  set echo off
      set serveroutput on size 1000000

      VARIABLE GV_return_code    NUMBER;
      VARIABLE GV_script_error   CHAR(255);
	  VARIABLE GV_process_flag   CHAR(10);

      EXEC :GV_return_code := 0;
      EXEC :GV_script_error := NULL;
	  EXEC :GV_process_flag := 'TRUE';

      WHENEVER SQLERROR EXIT ${FATAL}
      
      BEGIN
         BELK_NEW_ITEM_LOC_PRICING($1,$2,:GV_script_error,:GV_process_flag);
		 
	  
      EXCEPTION
         
         WHEN OTHERS THEN
		     ROLLBACK;
             :GV_return_code := ${FATAL};
      END;
        
	 
	   print :GV_process_flag;
       print :GV_script_error;
	   exit  :GV_return_code;
       /" | sqlplus -s ${connectStr} >> ${errTemp}
	   
    if [ $? -eq ${FATAL}} ]; then
	
      echo "${pgmName}:FAILED at EXEC_BELK_NEW_ITEM_LOC_PRICING() with error ${errTemp} !" >>${ERRORFILE}
      
      LOG_MESSAGE "${pgmName}:EXEC_BELK_NEW_ITEM_LOC_PRICING() Failed !"
    
      return ${FATAL}
	fi
	
		
}

But $1 and $2 is being passed as EMPTY.

---------- Post updated at 02:26 PM ---------- Previous update was at 02:23 PM ----------

But I was reading an article which says giving .(dot) runs the called_script in the same process ID whereas /path/called_script.sh runs in its own process ID which is different than the caller script's PID.

The problem is that within the function EXEC_BELK_NEW_ITEM_LOC_PRICING
in your script $1 and $2 will be the positional parameters to the function not the script. You could try:

#!/bin/ksh

priceA=$1
priceB=$2

...

function EXEC_BELK_NEW_ITEM_LOC_PRICING
{
   echo "
    ....

    BEGIN
    BELK_NEW_ITEM_LOC_PRICING($priceA,$priceB,:GV_script_error,:GV_process_flag);
    ... 
    " | sqlplus -s ${connectStr} >> ${errTemp}
...
}

. (dot) only runs in current process ID, if you use it as command (on it's own), this is could "sourcing" because the single dot can also be replaced with the keyword source examples:

. /path/to/script
. ./script

source /path/to/script
source ./script

dot-space means, to the shell, "run it in the current shell".

dot-slash means, to the operating system, "the current directory". It has no special meaning to the shell.

Thank you for finally posting your real script, which has revealed the actual problem.