I have written a script to ssh remote hosts, execute commands, save output to files, and examine outputs. But it always exit silently at line (( success++ )) when iterate first item in array workers. If I replace (( success++ )) with echo "process $worker", it will work fine and print all hosts. I cannot figure out what's wrong.
#!/bin/bash
set -x
set -e
workers=('host-1' 'host-2' 'host-3')
output_dir=$(mktemp -d)
for worker in ${workers[@]}; do
ssh $worker '
echo abc
echo OK
' > "$output_dir/$worker" &
done
echo "waiting..."
sleep 3
wait
success=0
regexp='OK$'
for worker in ${workers[@]}; do
output=`cat "$output_dir/$worker"`
if [[ "$output" =~ $regexp ]]; then
(( success++ ))
fi
done
echo "Total ${#workers[@]}; success: $success; failure: $((${#workers[@]} - success))"
if grep -q "$regexp" "$output_dir/$worker"; then? Or evengrep -c "$regexp" "$output_dir"/*to get a count of the number of OKs. Also considersuccess=$(( success + 1 )).