5

I want to write a Bash-Script which loggs into several machines via ssh and first shows their hostname and the executes a command (on every machine the same command). The hostname and the output of the command should be displayed together. I wanted a parallel version, so the ssh-commands should be run in background and in parallel.

I constructed the bashscripted attached below. The problem is: As the runonip-function is executed in a subshell, it got no access to the DATA-array to store the results. Is it somehow possible to give the subshell access to the Array, perhaps via a "pass by reference" to the function?

Code:

 #!/bin/bash
set -u

if [ $# -eq 0 ]; then
   echo "Need Arguments: Command to run"
   exit 1
fi 

DATA=""
PIDS=""

#Function to run in Background for each ip
function runonip {
    ip="$1"
    no="$2"
    cmds="$3"
    DATA[$no]=$( {
        echo "Connecting to $ip"
        ssh $ip cat /etc/hostname
        ssh $ip $cmds
    } 2>&1 )
}

ips=$(get ips somewhere)

i=0
for ip in $ips; do
    #Initialize Variables
    i=$(($i+1))
    DATA[$i]="n/a"

    #For the RunOnIp Function to background
    runonip $ip $i $@ &

    #Save PID for later waiting
    PIDS[$i]="$!"
done

#Wait for all SubProcesses
for job in ${PIDS[@]}; do
    wait $job
done

#Everybody finished, so output the information from DATA
for x in `seq 1 $i`; do
    echo ${DATA[$x]}
done;
theomega
  • 31,591
  • 21
  • 89
  • 127

3 Answers3

5

No, it's really not. The subshell runs in an entirely separate operating system process, and the only way for two processes to share memory is for their code to set that up explicitly with system calls. Bash doesn't do that.

What you need to do is find some other way for the two processes to communicate. Temporary files named after the PIDs would be one way:

#For the RunOnIp Function to background
runonip $ip $i $@ >data-tmp&
mv data-tmp data-$!

And then cat the files:

#Everybody finished, so output the information from the temp files
for x in ${PIDS[@]}; do
    cat data-$x
    rm data-$x
done;
Peter Westlake
  • 4,894
  • 1
  • 26
  • 35
4

You might be able to set up a named pipe to do interprocess communication.

Another possibility, in Bash 4, might be to use coprocesses.

Additional references:

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0

How to manually pass an associative array to and from a subprocess in Bash

Is it somehow possible to give the subshell access to the Array, perhaps via a "pass by reference" to the function?

Yes, it's possible. If you are really motivated, you can manually pass an array to and from a subprocess, using the techniques I show here: Manual passing (via serialization/deserialization) of the associative array.

I have a full demo for passing the array in to the subprocess. If you want the subprocess to modify the values and pass the array back out, that's a little harder, but you just need to separate the length, indices (keys), and values by some parsable separator, such as | or something. Ex: have the subprocess print everything out to stdout as:

#        length     indices (keys)  values,     separated by |
echo "${#array1[@]}|${!array1[@]}|${array1[@]}"

Then, capture this output like this: output="$(call_to_my_subprocess_func)", and parse output, looking for | to identify the length, indices, and values. Then, reconstruct the array on the outside the same as I show inside the print_associative_array func in the answer above.

It's not pretty, but it totally works.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265