heh, ChatGPT solves this from the exact prompt in a fairly reasonable way. After coming up wiht the pattern, I asked it to do it in bash, and then generalize it by having the script accept a list of commands.