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!

script sourcing issue.

Status
Not open for further replies.

rakeshou

Programmer
Aug 15, 2006
17
US
I have a script say abc.sh which needs to be executed by another script as

. ../abc.sh

but the control never returns to the calling script. The control does come back if I use
../abc.sh

but then abc.sh is run as child script.

Please tell me how do I bring the control back to the calling script so that the calling script can read the exported variables from the called script.


abc.sh (just for demonstration) is as under:

#/bin/ksh -x

DEFAULT_DB=tektips
export DEFAULT_DB

if [ $DEFAULT_DB = "tektips" ]; then
exit 0
else
exit 1
fi
 
To start, your first line is wrong. You're missing an exclamation point ("!"). Also, this line isn't executed when the file is sourced, only when the script is run as it's own process.

You should also change the "exit"s to "return"s.
Code:
#!/bin/ksh -x

DEFAULT_DB=tektips
export DEFAULT_DB

if [ $DEFAULT_DB = "tektips" ]; then
   return 0
else
   return 1
fi
Still, you can only set a variable in a script if it's being sourced.
 
The reason it doesn't return when you are sourcing it is because of the "exit"s. Since you are sourcing it, the "exit" is run by the calling script, not abc.sh, and causes the calling script to exit. So the "exit" kills the parent and just appears to not return.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top