Jump to content

Variables passed through to Scripts [SOLVED]


Recommended Posts

I have a situation whereby i call a script within a script, within the current shell.. i.e:

 

 

SCRIPT1.sh:

 

. ./path/to/script/script2.sh

 

SCRIPT1.sh is one of many script we run and can do a range of things, script2.sh is set up to return three variables from it USERNAME, PASSWORD and DATABASE. The USERNAME and PASSWORD generated in script 2 depends on username of the user running the script OR a passed variable (i.e. $1).

 

All of the above works so:

 

SCRIPT1.sh could call script2.sh as follows:

 

. ./path/to/script/script2.sh DIFFUSER

 

The script2 is run within script1's CURRENT shell as to allow SCRIPT1 access to the values passed back.

 

The problem, if i have the following script1:

 

SCRIPT1.sh

#---

echo $1

echo $2

 

. ./path/to/script/script2.sh DIFFUSER

 

echo $USERNAME

echo $PASSWORD

echo $DATABASE

 

echo $1

echo $2

#---

 

run like:

SCRIPT1.sh VALUE1 VALUE2

 

It all works as expected BUT on the second echo of $1 in SCRIPT1 the value is altered to DIFFUSER from VALUE1.

 

This is not acceptable.

 

Now the constraints i have is:

 

. ./path/to/script/script2.sh

 

has to be able to run without requiring any parameters (default)

 

It has to run with a parameter passed to it to allow it to pick up new usernames, passwords, DB based on the passed variable.

 

I have got a workaround to this by doing the following:

 

SCRIPT1.sh

#---

echo $1

echo $2

 

. ./path/to/script/script2.sh ${1:-1} ${2:-2} DIFFUSER

 

echo $USERNAME

echo $PASSWORD

echo $DATABASE

 

echo $1

echo $2

#---

 

But if the SCRIPT1 variants ever have more than 2 parameters passed to them then script2 will mess things up.

 

I can not alter the way the script2 is called, i can alter script2 though..

 

Ideas (script 2 does some processing then exports the variables, and has to be run as ". ./path/script2.sh" i.e. in the current shell.

 

Ideas?

Link to comment
Share on other sites

sorted the problem.. used the following:

 

---script2.sh---
function checkparam {

PARMS=$*

server=`echo $PARMS | awk ' { print $NF } '`
checkparm=`echo $server | awk -F_ ' { print ( $1 ) } ' `
case ${checkparm} in
  "CHECKPARAM")  export var_server=${server};;
                 *)  export var_server=${UKSSP}
esac


}

checkparam $*

echo "var_server = $var_server"

 

when we pass a value to the script2.sh now we use . ./path/script2.sh $* PARAM

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...