0

I have two bash scripts running simultaneously, which are identical apart from two things.

Apart from a few other setup pieces they run the following entry:

ssh ${target_server} "bash -s" < ${target_script} > ${home_dir}/logs/${instance_name}.log 2> ${home_dir}/logs/${instance_name}.err &

The only differences between the two scripts are the IP Address passed into the ${target_server} variable and the ${instance_name} variable.

So expanded it would look something like this:

In local_script1:

target_script="target_script1.sh"
ssh 10.1.2.3 "bash -s" < ${target_script} > /home/user/logs/script1.log 2> /home/user/logs/script1.err &

In local_script2:

target_script="target_script2.sh"
ssh 10.1.2.4 "bash -s" < ${target_script} > /home/user/logs/script2.log 2> /home/user/logs/script2.err &    

The target_scripts are again two separate bash files which are identical apart from a few key variables relating to paths on the relevant server.

Is it possible, that although they are running on separate servers, that there is some crossover of the variables being set.

e.g. if I set myVar=1 in target_script1 could it be picked up by target_script2?

Or (am I right in thinking) that because each one is opening an SSH connection to the 'remote' server first the variables ONLY exist on the remote servers, and not the local one.

  • You will invoke different instances of the shell. And variables can't "travel" between subshells. If you define it in master shell and export it will be visible in subshells. – Romeo Ninov Sep 30 '16 at 9:47
  • Ok, so anything in local_script1 IS visible to local_script2. But anything in remote_script1 IS NOT visible to remote_script2 ? – IGGt Sep 30 '16 at 9:54
  • How do you invoke your local scripts? – glenn jackman Sep 30 '16 at 10:03
  • All scripts have #!/bin/bash at the start, and then they are run as two separate Cron Jobs every 5 minutes. */5 * * * * /home/user/.../local_script1.sh > ... – IGGt Sep 30 '16 at 10:06
0

No, variables set within one script are not visible in another script. Both scripts are executing in their own separate environments.

Example:

bash -c 'a=1; echo $a'

bash -c 'echo $a'

The second invocation of bash will never ever write a 1 as output, no matter in which order the two lines are run or if they execute concurrently.

The only case where the second line would output 1 is if there is an environment variable (exported shell variable) called a in the environment calling it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.