Bash: Subprocess access variables

2020-03-24 04:35发布

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;

2条回答
一夜七次
2楼-- · 2020-03-24 05:18

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;
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-03-24 05:29

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:

查看更多
登录 后发表回答