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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Passing Parameters to Script 3

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

In a Bourne shell script I am calling an include file where I am trying to add the ability of the called script to act upon parameters being passed.

The include script runs OK by itself being called from the command line but not when being called from within my parent script. It does not seem to be getting the parameters or setting\exporting the variable.

Parent Script
Code:
#!/bin/sh
. /mypath/initVars.sh DB1

[COLOR=green]# The value of this is set in initVars.sh  
# but is empty when running from this script.[/color]
echo $MYDB

initVars.sh (this works fine being executed by itself)
Code:
#!/bin/sh
# Usage:   initVars.sh <db_name>
# Example: initVars.sh DB1  
MYDB="$1"
export MYDB
echo $MYDB

What can you suggest?

Thanks,

Michael42
 

Use either bash --or-- ksh:

Code:
#!/bin/bash
#--or--
#!/bin/ksh

[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA,

>> Use either bash --or-- ksh:

I am sorry to say this is not an option for me right now.


Thanks for posting,

Michael42

 
And what about this ?
#!/bin/sh
set -- DB1
. /mypath/initVars.sh
echo $MYDB

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It appears that sh doesn't expect or handle any parameters when you source a script using ".", so you're out of luck there I think.

Annihilannic.
 
You could try a shell function:

------/mypath/sh_fun_lib.sh-----
#!/bin/sh
F_Init_Vars ()
{
MYDB=$1
export MYDB
}
--------------------------------

------/your/script.sh-----------
#!/bin/sh
###
#install shell functions
. /mypath/sh_fun_lib.sh
###
# set vars
echo before MYDB=$MYDB
F_Init_Vars DB1
echo after MYDB=$MYDB
---------------------------------

I just typed this blindly, not at a shell session right now. So not sure it works (or maybe there's an error) - but I tried a similar thing in ksh a while ago and that worked...


HTH,

p5wizard
 
p5wizard,

Thanks for posting - that really works well! :)

Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top