The UNIX and Linux Forums  


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
Passing variables between scripts... Jazmania UNIX for Dummies Questions & Answers 1 09-19-2007 06:48 AM
Passing variables to sed jfisch Shell Programming and Scripting 3 03-07-2005 07:25 AM
Passing awk Variables gozer13 Shell Programming and Scripting 3 01-04-2005 04:32 PM
passing variables sounder123 Shell Programming and Scripting 1 06-10-2004 10:19 AM
Passing Variables to AWK AreaMan Shell Programming and Scripting 5 01-28-2002 06:30 PM

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

Join Date: Jan 2008
Posts: 3
Passing variables between scripts

Hi all.

I need to pass a value from a script that runs in a sub-shell, back into the calling shell like below (or into script 2 directly):

outer_script
export param=value1

script1 $param
(in script1: export param=value2)

script2 $param
($param is now value1, not value2 like i'd prefer)
end outer_script

I can't get this to work using regular variables and I have read some about shells and sub-shells and variables and I now believe that it's not possible with variables.

I guess I could always use a file to store the value in script1 and then read it in script2 using the file. But if I go this way I'd be forced to have separate files for each user that executes the script so that no conflicts occurr. But then I'd get a lot of files that mess up. I guess I could then remove each file after the script completes but what if the user terminates using Ctrl+C? I'd still have a lot of messy files after a while.

At the moment I'm leaning towards using files, but I thought I'd ask if someone knows another/better way to do this without using files?

This is in Korn Shell
  #2 (permalink)  
Old 01-11-2008
V3l0 V3l0 is offline
Registered User
  
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
In your script1, you have to return the value, beacause the export is lost at the end of script.

At the end of 'script1', do 'exit $param'

And try this :

Code:
outer_script
export param=value1

param=$(script1 $param)
(in script1: export param=value2)

script2 $param
($param is now value1, not value2 like i'd prefer)

end outer_script
  #3 (permalink)  
Old 01-11-2008
rein rein is offline
Registered User
  
 

Join Date: Dec 2004
Location: Zürich
Posts: 146
Or try with mkfifo.

Open a queue for reading in the first script, then call second script and write to the queue from the second script. The first script will wait untill it has read something, then continue.
  #4 (permalink)  
Old 01-12-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Use sourcing

In order to have the value of a variable from a different script run within a another script, available globally, you need to source the inner script, instead of simply executing it.

Code:


outer_script

export param=value1

source /path/to/script1 
# or use the dot symbol :    .  /path/to/script1
(in script1: export param=value2)

script2 $param
# Now $param will be value2

end outer_script


# Now the new value (value2) of the variable param will be set globally.
Google source command for more info.
  #5 (permalink)  
Old 01-14-2008
bbergstrom74 bbergstrom74 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Quote:
Originally Posted by rubin View Post
In order to have the value of a variable from a different script run within a another script, available globally, you need to source the inner script, instead of simply executing it.

Google source command for more info.
Thanks a lot, just what I needed!!
Closed Thread

Bookmarks

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 02:15 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