Skip to main content
added 22 characters in body
Source Link
dimo414
  • 2k
  • 1
  • 18
  • 39

I'd like to run a series of tasks, but stop should any of them fail, hence I've written (something like):

for task in [TASKS]; do
  process "$task" || break
  commit "$task"
done

This works fine, but (as specified) the exit status of this loop is zero even if we break early. Ideally break-ing would be able to convey the failure.

I know returning 0 is the documented behavior of break, but I'm curious if there are any relatively clean workarounds. The best I can imagine is wrapping this in a function and setting a didBreak variable, and using that as the exit status (of the function). It'd work, but it feels overly-complex.

I'd like to run a series of tasks, but stop should any of them fail, hence I've written (something like):

for task in [TASKS]; do
  process "$task" || break
done

This works fine, but (as specified) the exit status of this loop is zero even if we break early. Ideally break-ing would be able to convey the failure.

I know returning 0 is the documented behavior of break, but I'm curious if there are any relatively clean workarounds. The best I can imagine is wrapping this in a function and setting a didBreak variable, and using that as the exit status (of the function). It'd work, but it feels overly-complex.

I'd like to run a series of tasks but stop should any of them fail, hence I've written (something like):

for task in [TASKS]; do
  process "$task" || break
  commit "$task"
done

This works fine, but (as specified) the exit status of this loop is zero even if we break early. Ideally break-ing would be able to convey the failure.

I know returning 0 is the documented behavior of break, but I'm curious if there are any relatively clean workarounds. The best I can imagine is wrapping this in a function and setting a didBreak variable, and using that as the exit status (of the function). It'd work, but it feels overly-complex.

Source Link
dimo414
  • 2k
  • 1
  • 18
  • 39

Trigger a non-zero exit code when I break out of a loop

I'd like to run a series of tasks, but stop should any of them fail, hence I've written (something like):

for task in [TASKS]; do
  process "$task" || break
done

This works fine, but (as specified) the exit status of this loop is zero even if we break early. Ideally break-ing would be able to convey the failure.

I know returning 0 is the documented behavior of break, but I'm curious if there are any relatively clean workarounds. The best I can imagine is wrapping this in a function and setting a didBreak variable, and using that as the exit status (of the function). It'd work, but it feels overly-complex.