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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing a variables value from the called script to calling script using ksh
# 1  
Old 06-18-2008
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 [ $? != 0] then
# error processing
# I want to use the $PID(from the script2 here)

Portion of Script 2

# few processing

If I use return (exit PID), the problem is, I can not check the script failure or success.
I want to use only the variable PID in script1 not the other variables.

Thanks for your help.

Last edited by rajarkumar; 06-18-2008 at 09:02 PM..
# 2  
Old 06-19-2008
You have this in your script:


Just add the following:

export Pid_script2

Pid_script2 ia a global variable now.

PS: I am assuming script2 runs first.Also you will have to source script1 and script2 so that it does not run in another shell.
# 3  
Old 06-19-2008
Thanks for the reply.

Actually script1 runs first and calls script2. If you look at the code sample above.

Problem is returning only one variable from script2 at the same I should be able to check the script2 run status using $?.

You can have all the variable by executing the script2 inside script1 using . scriptname (inline). The problem is I've same set of variables in script1 and script2, both uses another file for sourcing the common variables.
# 4  
Old 06-19-2008
Have the child script open a file, write whatver you want defined., like this:
#inside child script
echo "PID=$mypid" > /tmp/pidfile
# the pid of the child is now recorded in /tmp/pidfile

#parent process
tmp=$(child.ksh 1 2 3 )
# now read the file child.ksh wrote
chmod +x /tmp/pidfile 
. /tmp/pidfile
# The variable PID is now defined in the parent

The only other way to do this is using co-processes, which do not seem to match your model.
# 5  
Old 06-19-2008
You could "print" the value to the standard output in script 2:
print $PID or echo $PID
In script1 get it to a variable:
tmp=`script2 p1 ...`
The tmp variable will have your value.
You can do the same if you have to return more than 1 value.
They will be all in the tmp variable separated by space. To use them put them into "parameters" (don't forget to save true parameters of script 1):
set $tmp
Now $1 will have the return value 1, $2 - the return value 2, etc.
# 6  
Old 06-19-2008
I understand the concept of writing it to standard output and assign the value to a variable. Can you show me some code sample to display $1 $2 of the variable ?
# 7  
Old 06-19-2008
Jim, Your suggestion is a good workaround.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Weird issue - *ksh script not recognized when being called

HI Team - I'm running into a weird issue when trying to call a .ksh script. In my shell script, I'm using the following command to call my environment file: cd /hypbin/test ./_env.ksh But it's saying not found. Permissions are set correctly, shebang is set but I'm unsure why it's not... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

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

3. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

4. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: LoneRanger
6 Replies

5. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script( from a master script( to get string type value from calling script to master script. I have used scripts mentioned below. ./ echo $fileExist echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

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

7. UNIX and Linux Applications

Calling script - Called script concept.

Hi Guys, I am new to unix and would like to know the basics about calling a shell script from another shell script. I would like to know if the calling script can finish in any way before the called script actually gets executed completely. I want this to happen :cool: For Example :... (1 Reply)
Discussion started by: khedu
1 Replies

8. Shell Programming and Scripting

How to return the value from the called shell script to the calling sh script

Hi all, I have two ksh scripts #!/bin/ksh . ./ echo $fileExist #!/bin/ksh func() { i=1 return $a } func echo $? Here how should I return the value of back to Thanks in advance. (2 Replies)
Discussion started by: gp_singh
2 Replies

9. Shell Programming and Scripting

Need help with ksh script that uses sqlplus, called from PHP

I have a ksh script that connects to sqlplus and dumps the query results into a file. The script works file when I run it from the command line, however, when I call it from PHP using system(), exec(), or shell_exec() commands, the script doesn't seem to run the query. It will create the text file... (7 Replies)
Discussion started by: j2owilson
7 Replies

10. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies
Login or Register to Ask a Question

Featured Tech Videos