#!/bin/bash
your_func()
{ # use your cmd or func instead of sleep here. don't end with background(&)
date +%s
echo "scp HOSTNAME:/home/USER/path/from/file.$1 REMOTE_HOST:/home/USER/path/to/"
sleep 2
}
concurrent()
{ # from $1 to $2, (included $1,$2 itself), con-current $3 cmd
start=$1 && end=$2 && cur_num=$3
# ff_file which is opened by fd 4 will be really removed after script stopped
mkfifo ./fifo.$$ && exec 4<> ./fifo.$$ && rm -f ./fifo.$$
# initial fifo: write $cur_num line to $ff_file
for ((i=$start; i<$cur_num+$start; i++)); do
echo "init time add $i" >&4
done
for i in a.sh b.sh c.sh ; do
read -u 4 # read from mkfifo file
{ # REPLY is var for read
#echo -e "-- current loop: [cmd id: $i ; fifo id: $REPLY ]"
sh $i
echo "real time add" 1>&4 # write to $ff_file
} & # & to backgroud each process in {}
done
wait # wait all con-current cmd in { } been running over
}
concurrent 0 8 3