Passing variable from called script to the caller script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variable from called script to the caller script
# 1  
Old 09-22-2014
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.

Code:
#!/bin/ksh

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

#some other operations

called script
Code:
#!/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
# 2  
Old 09-22-2014
If I replace 'sqlplus' with 'cat' it obviously works:

Code:
$ 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 ...
# 3  
Old 09-22-2014
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?
# 4  
Old 09-22-2014
No, it does not.

Either these aren't your actual scripts, or the error lies elsewhere.
# 5  
Old 09-22-2014
This is being called like this

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


##Inside belk_nil_prc_thread.ksh



Code:
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 ----------

Quote:
Originally Posted by Corona688
No, it does not.

Either these aren't your actual scripts, or the error lies elsewhere.
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.
# 6  
Old 09-22-2014
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:

Code:
#!/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:

Code:
. /path/to/script
. ./script

source /path/to/script
source ./script

# 7  
Old 09-22-2014
Quote:
Originally Posted by LoneRanger
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.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Programming

Remote login UNIX box from java passing parameters to the custom script called in the profile

Hello Good Day / Guten Tag.... I have to login the server and the user profile contains some scripts which need the inputs to be taken from the keyboard. So I use the method to conn.authenticateWithKeyboardInteractive(username, new InteractiveCallback() { public String... (1 Reply)
Discussion started by: Sanalkumaran
1 Replies

3. Shell Programming and Scripting

Passing stdin value into a script that is called from another script

I'm trying to automatically pass user input values into a script that is being called from another script, below is my current script and I added a comment next to the script where it asks user to enter input value. Thanks, mbak #!/bin/ksh echo " Adding disks for DB server then Enter YES... (2 Replies)
Discussion started by: mbak
2 Replies

4. Shell Programming and Scripting

Passing the value of variable which is read from command line in called script

Hi, I am calling a Perl script in my shell script. When Perl script is executed it asks for a answer to be entered by user from terminal. How can i pass that value from my shell script ?? I know I can change perl script to default the answer but i dont have access to do that so only option i... (5 Replies)
Discussion started by: varun22486
5 Replies

5. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. UNIX for Dummies Questions & Answers

How to retrieve the value of variable in shell script which is called by crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (4 Replies)
Discussion started by: jadoo_c2
4 Replies

7. Shell Programming and Scripting

environment variable in shell script called through crontab

Please help me on below.. https://www.unix.com/shell-programming-scripting/141533-retrieve-value-environment-variable-shell-script-called-crontab.html#post302442024 I'm still here. I can still see you! (0 Replies)
Discussion started by: jadoo_c2
0 Replies

8. Shell Programming and Scripting

Retrieve the value of environment variable in shell script which called from crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (0 Replies)
Discussion started by: jadoo_c2
0 Replies

9. Shell Programming and Scripting

Caller Script should produce and email from 4 different script within it

Hi I wrote a shell script , which includes one output file which is emailed back to me , when the script runs . Now i want to slip the script into 4 different shell scripts each of which taking the parameter PROD or DEV, and include all the 4 different shell scripts in a caller script. ... (3 Replies)
Discussion started by: rxg
3 Replies

10. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies
Login or Register to Ask a Question