Suppose I have two space-delimited strings in my bash script, which are
permitted_hosts="node1 node2 node3"
and
runs_list="run1 run2 run3 run4 run5"
These respectively represent the list of permitted hosts and the list of runs to execute. So, I need to run each of the runs in $runs_list on 1 of the hosts in $permitted_hosts.
What I'd like to do is divide $runs_list into $N substrings, where $N is the number of elements in $permitted_hosts and where each of the $N substrings is mapped to a different element in $permitted_hosts.
If that's confusing, then consider this concrete workaround solution. For the exact given values of $permitted_hosts and $runs_list above, the following bash script checks the current host, and if the current host is in $permitted_hosts, then it launches the runs in $runs_list that are associated with the current host. Of course, this script doesn't use the variables $permitted_hosts and $runs_list, but it achieves the desired effect for the given example. What I'm really trying to do is modify the code below so that I can modify the values of variables $permitted_hosts and $runs_list and it will work appropriately.
#!/bin/bash
hostname=$(hostname)
if [ "$hostname" == "node1" ]; then
runs="run1 run2"
elif [ "$hostname" == "node2" ]; then
runs="run3 run4"
elif [ "$hostname" == "node3" ]; then
runs="run5"
else
echo "ERROR: Invoked on invalid host ('$hostname')! Aborting."
exit 0
fi
for run in $runs; do
./launch $run
done