The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
url calling and parameter passing to url in script gander_ss Shell Programming and Scripting 1 05-12-2008 09:45 PM
url calling and parameter passing to url in script gander_ss UNIX for Advanced & Expert Users 1 05-07-2008 12:34 PM
Passing a parameter while calling a script Rafael.Buria Shell Programming and Scripting 2 02-13-2008 06:27 AM
Passing exit code to calling script debbiekuch Shell Programming and Scripting 2 09-18-2006 09:46 AM
passing variables to awk from ksh script rein Shell Programming and Scripting 3 08-11-2005 11:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-18-2008
rajarkumar rajarkumar is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
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
fi
-----
-----
# I want to use the $PID(from the script2 here)

Portion of Script 2
=============

-----
-----
# few processing
PID=SOMEVALUE ASSIGNED


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 07:02 PM..
  #2 (permalink)  
Old 06-19-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
You have this in your script:

PID=SOMEVALUE ASSIGNED

Just add the following:

Code:
Pid_script2=$PID
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 (permalink)  
Old 06-19-2008
rajarkumar rajarkumar is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
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 (permalink)  
Old 06-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Have the child script open a file, write whatver you want defined., like this:
Code:
#inside child script
mypid=$$
echo "PID=$mypid" > /tmp/pidfile
# the pid of the child is now recorded in /tmp/pidfile
Code:
#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 (permalink)  
Old 06-19-2008
odashe odashe is offline
Registered User
  
 

Join Date: May 2005
Posts: 31
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 (permalink)  
Old 06-19-2008
rajarkumar rajarkumar is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
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 (permalink)  
Old 06-19-2008
rajarkumar rajarkumar is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
Jim, Your suggestion is a good workaround.
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0