set -e did not help
An operator script started with the familiar set -euo pipefail. It found
a Docker Swarm task and was about to run a command in its container.
The check for exactly one task was delegated to awk:
set -euo pipefail
read -r node task < <(
printf 'node-a task-a\nnode-b task-b\n' |
awk '{ print; n++ } END { if (n != 1) exit 1 }'
) || exit 1
printf 'continued: node=%s task=%s\n' "$node" "$task"
There are two tasks. awk correctly returns an error, while the script happily
prints continued: node=node-a task=task-a and exits with status 0.
Reproduced on Bash 3.2.57.
The catch is <(...): the process inside runs asynchronously, and its exit
status does not become the exit status of read. That command has read the
first line and is satisfied. The outer || exit 1 checks read; pipefail
applies to the pipeline inside the substitution. The Bash documentation
describes this behavior.
We changed the order: obtain the complete list and check for failure, count
all live tasks, require one in Running state, and only then parse the line.
We also added --no-trunc: the shortened task ID
could not locate the container. The operator now supplies the node, and the
script stops if it does not match.
Test this with an ssh stub: two tasks, Starting, an empty list, and another
node. Every case must fail before docker exec. An error message alone is
not enough. The example above has an error too. It just does not stop anyone.