In the shell script below, I would like to run 2 commands parallel in the background to speed up the process, wait for them to finish and continue to run some more commands afterwards.
As I am using the same variable name (DATASERVERNAME) in both loops, are these variables going to interfere with each other in the background? Should I use different variable names, like DATASERVERNAME_SYBASE, DATASERVERNAME_ORACLE in each loop instead?
#!/bin/bash
while read DATASERVERNAME
do
some commands here
done < sybase_data_servers.txt &
while read DATASERVERNAME
do
some commands here
done < oracle_data_servers.txt &
wait
some more commands here
Your script is safe as you could reveal through a little experiment:
#!/bin/sh -eu
echo A > a
echo B > b
X=0
while read X
do
echo X=$X
done < a &
wait
echo X=$X
while read X
do
echo X=$X
done < b &
wait
echo X=$X
Script output:
X=A
X=0
X=B
X=0
The backgrounded loops run in different processes ($BASHPID
should reflect that too).