LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash programming question - hand over variables to another script (https://www.linuxquestions.org/questions/linux-general-1/bash-programming-question-hand-over-variables-to-another-script-619893/)

ppr:kut 02-09-2008 07:48 AM

bash programming question - hand over variables to another script
 
Hey guys!

I searched the internet over and over, but couldn't find a solution, basically because of not knowing exactly how I should entitle it :/. I try to describe. I have the following script (just a test case):

Code:

#!/bin/bash
TEST=${TEST:-nope}
echo $TEST

I want to call this script from another one, changing the TEST variable via commandline parameter. So something like that:

wrapper.sh TEST=yes

wrapper.sh:
Code:

#!/bin/bash
SCRIPT="/home/user/test.sh"
$1 $SCRIPT

I already know that this code, as it's written here, does not work. IS there any solution to this, or does it simply not work?

jschiwal 02-09-2008 08:18 AM

Your sample program doesn't match the question you posted.

It performs the command you pass as an argument to the script.

Here is a similar example:
Code:

cat indprog.sh
#!/bin/bash

$1 visudo.tex

jschiwal@hpamd64:~> ./indprog.sh wc
 199  912 7304 visudo.tex
jschiwal@hpamd64:~> ./indprog.sh ls
visudo.tex

If you want a script to create a variable, source the script; and in the script, export the variable.

Code:

cat >testprog.sh
#!/bin/bash
VAR1='ABC123'
export VAR1
jschiwal@hpamd64:~> chmod +x testprog.sh
jschiwal@hpamd64:~> ./testprog.sh
jschiwal@hpamd64:~> echo $VAR1

jschiwal@hpamd64:~> . ./testprog.sh
jschiwal@hpamd64:~> echo $VAR1
ABC123

Or you could have the program output the value:
VAR1=$(./script)

Is this what you had in mind?
Code:

cat test2.sh
echo -n 'VAR2=TEST2'
jschiwal@hpamd64:~> V=$(./test2.sh )
jschiwal@hpamd64:~> eval $V
jschiwal@hpamd64:~> echo $VAR2
TEST2


tronayne 02-09-2008 08:25 AM

If you change test.sh to
Code:

#!/bin/bash
TEST=${1:-nope}
echo ${TEST}

and wrapper.sh to
Code:

#!/bin/bash
SCRIPT=test.sh
${SCRIPT} ${1}

then it'll do what I think you want it to.

ppr:kut 02-09-2008 08:46 AM

Thanks for the fast responses :).

Quote:

Your sample program doesn't match the question you posted.
Well, yes. As I said above, part of my problem is I don't know how to entitle it.

Quote:

It performs the command you pass as an argument to the script.
Exactly! And that is my problem. I call "wrapper.sh TEST=yes" and the output I'm waiting for is "yes".
Instead I get "TEST=yes: command not found"

I would like to tell bash somehow, that $1 isn't a command, but a variable I want to pass on to test.sh

tronayne:
Almost. But fact is, I can't change test.sh (for my purpose). And I do really need to pass the variable before the actual script.

tronayne 02-09-2008 09:39 AM

Quote:

Almost. But fact is, I can't change test.sh (for my purpose). And I do really need to pass the variable before the actual script.
OK, you've got two choices: export TEST in wrapper.sh or export TEST in your .profile file.

If you do the first, all child processes of wrapper.sh (and whatever any additional child processes are called from the children) will be able to use that variable; the variable will cease to exist on exit. If you do the second then any process in the stream (wrapper.sh, test.sh, etc., etc.) will be able to use or change the content of that variable and it will still available after everything exits; i.e., you'll be able to echo ${TEST} after everything runs and see the final value.

ppr:kut 02-09-2008 10:00 AM

Code:

#!/bin/bash
SCRIPT="/home/user/test.sh"
export $(echo $1)
$SCRIPT

Thanks very much. The above script does work :) .


All times are GMT -5. The time now is 07:19 PM.