Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variables from one script to the next

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
0
0
DK
Hi

I need a good way to pass variables from one script that is executede from another.

I have a script (subscript.ksh) defines a variable a=10
This subscript is executede by another script headscript.ksh that needs to use a.
What would be a good way to pass the variable along.

I ve tryede to export a byt the headscript.ksh does not catch it.

This is how the scripts look now.
headscript.ksh
Code:
#!/bin/ksh

SUBSCRIPT=$HOME/subscript.ksh

$SUBSCRIPT

echo $a

subscript.ksh
Code:
#!/bin/ksh

export a=10


This just does not work :(


Regards
Lhg
 
Try
Code:
#!/bin/ksh

SUBSCRIPT=$HOME/subscript.ksh

. $SUBSCRIPT

echo $a

The
Code:
. $SUBSCRIPT
ensures that subscript.ksh runs in the same shell, otherwise a new shell is created and all variables created within it are lost when it closes.

On the internet no one knows you're a dog

Columb Healy
 
The more usual way to get information from a subscript to a parent is for the subscript to simply echo some output to stdout, which the parent script can then capture or parse.

headscript.ksh:

Code:
#!/bin/ksh

SUBSCRIPT=$HOME/subscript.ksh

a=$($SUBSCRIPT)

echo $a

subscript.ksh:

Code:
#!/bin/ksh
# do some stuff
a=10
echo $a

Annihilannic.
 
Annihilannic
Your solution is fine as long as there's only one variable to set. We have multiple version of our production software running on the same box so, before working on any particular one, you need to set multiple environment variables. We therefore have a number of scripts 'set_env_instance_1', 'set_env_instance_2' etc. each of which contains settings for ORACLE_SID, ORACLE_HOME, PATH, etc.

For convenience these are aliased in .profile so that entering instance_1 runs
Code:
. ~/set_env_instance_1

Having said that, whilst I heavily endorse using this method for setting environment variables, I detest scripts which call other scripts to set variables in the way I have outlined. When you come to debugging them you never know exactly what they do and, later on in the parent script you're wondering 'How did $a get set' whereas your method at least makes that clear.

On the internet no one knows you're a dog

Columb Healy
 
Hi

If the second script is written in a careless manner, [tt]source[/tt]ing it could harm. ( Like unexpected [tt]exit[/tt], overwriting the first script's global variables. ) In which case [tt]eval[/tt] may help :
Code:
#!/bin/ksh

SUBSCRIPT="$HOME/subscript.ksh"

eval "$( "$SUBSCRIPT" )"

echo "$a"
echo "$b"
echo "$c"
Code:
#!/bin/ksh

a='10'
echo "a='$a'"

b='Hello World'
c="$a$a"

echo "b='$b'"
echo "c='$c'"
But of course is possible to harm even more...

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top