I want a script which will restart the defined services on their respective servers. I want to pass parameter as below to the script: eg:
sh execute.sh [server1:nginx,mysqld],[server2:mysqld,apache2],[server3:mongodb,apache2]
So it should go to server1 and restart nginx and mysqld service over there. Then to server2 and should restart mysqld and apache over there and so on so.
I have the script like below:
#!/bin/bash
# create an array of server:services
a=($(echo "$1" | gawk 'BEGIN { FS="[]],[[]" } ; { print $1, $2, $3 }' | tr -d '[]'))
# add a for loop here to iterate values in array with code below
for var in "${a[@]}" ; do
# get server name
server1=$(echo $a[0] | cut -d ':' -f1)
# get your services as space separated
servs1="$(echo $a[0] | cut -d ':' -f2 | tr ',' ' ')"
# loop your services
for s in $servs1; do
  ssh $server1 "service $s restart"
done
done
The above script is only able to grep the first server name and service. Please help to get others.

restart server1:nginx,mysqld server2:mysqld,apache server3:mongodb,apaches)