Passing Variable from one script to another

5,603

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"
Share:
5,603

Related videos on Youtube

IGGt
Author by

IGGt

Updated on September 18, 2022

Comments

  • IGGt
    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 executes test2.sh. But all I am getting is varName is 123456: No such file or directory

    • IGGt
      IGGt over 8 years
      well spotted. . . that was a typo
    • IGGt
      IGGt over 8 years
      test1.sh and test2.sh exist only on the main server, However, test2.sh contains code that connects to a database and runs some queries. The varname variable(s) will contain database connection details amongst others.
  • IGGt
    IGGt over 8 years
    cheers, by that gives me bash: test2.sh: command not found
  • David King
    David King over 8 years
    Is test2.sh located on the local machine or the remote one?
  • IGGt
    IGGt over 8 years
    the local machine (same place as test1.sh)
  • David King
    David King over 8 years
    If you want to run test2.sh on the remote machine it should be located there.
  • IGGt
    IGGt over 8 years
    that's closer cheers, but not quite. I am just getting varname is with no numbers.
  • IGGt
    IGGt over 8 years
    The 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
    David King over 8 years
    See my edited test1.sh
  • Eric Renouf
    Eric Renouf over 8 years
    Hmm, 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
    IGGt over 8 years
    it 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
    IGGt over 8 years
    Ah ha, in test2.sh I still had varname is $1 instead of varname is $varname. It now works. Cheers for your help,