Passing Variable from one script to another
Solution 1
You're running into an "order of operations" type problem I think. The <
is processed before the commands are execute, so you're running something similar (though not identical to)
varname=$varname test2.sh | ssh [email protected] bash
which runs the test2.sh
locally, then sends that output to bash
on the remote server. That's why you see the output of test2.sh
but then get an error when it tries to execute that output as a command.
If you're running a script that's executable you don't really need the bash
there though, so you could just do
ssh [email protected] "varname=$varname ./test2.sh"
assuming test2.sh
is available on the remote server.
If you don't have test2.sh
on the remote server and still want to execute it there you could do it like so:
ssh [email protected] varname="$varname" bash < test2.sh
which will redirect the script source but do the variable processing in the context of the bash
executable
Solution 2
Why don't you just pass it as an argument. ie.
test2.sh
#!/bin/bash
echo "this is test2.sh"
echo "varName is $1"
echo end
test1.sh
#!/bin/bash
varName="123456"
scp test2.sh [email protected]:/tmp/
ssh [email protected] "/tmp/test2.sh $varName"
ssh [email protected] "rm -f /tmp/test2.sh"
Related videos on Youtube
IGGt
Updated on September 18, 2022Comments
-
IGGt almost 2 years
How can I pass a variable between scripts?
Example: I have two test scripts:
-- test2.sh #!/bin/bash echo "this is test2.sh" echo "varname is $varname" echo end -- tes1.sh #!/bin/bash varname="123456" ssh [email protected] "bash" < varname=$varname test2.sh
The idea is that I run
test1.sh
which connects to a remote server, and executestest2.sh
. But all I am getting isvarName is 123456: No such file or directory
-
IGGt over 8 yearswell spotted. . . that was a typo
-
IGGt over 8 years
test1.sh
andtest2.sh
exist only on the main server, However,test2.sh
contains code that connects to a database and runs some queries. Thevarname
variable(s) will contain database connection details amongst others.
-
-
IGGt over 8 yearscheers, by that gives me
bash: test2.sh: command not found
-
David King over 8 yearsIs
test2.sh
located on the local machine or the remote one? -
IGGt over 8 yearsthe local machine (same place as test1.sh)
-
David King over 8 yearsIf you want to run
test2.sh
on the remote machine it should be located there. -
IGGt over 8 yearsthat's closer cheers, but not quite. I am just getting
varname is
with no numbers. -
IGGt over 8 yearsThe problem is that there is apx 70 machines to run it on, and as the script is the same for all of them I don't want to try and maintain 70 copies of it.
-
David King over 8 yearsSee my edited
test1.sh
-
Eric Renouf over 8 yearsHmm, and you're sure you have the same capitalization on
varname
still, right? Because when I test this execution with a simple script I'm getting the value printed on the other side -
IGGt over 8 yearsit is. I added
echo test1 varname is $varname
to test1.sh just to make sure it was being set properly, and that works fine. It just doesn't copy across. -
IGGt over 8 yearsAh ha, in test2.sh I still had
varname is $1
instead ofvarname is $varname
. It now works. Cheers for your help,