Skip to main content
Thank you mosvy
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

The documentation for xargs (see man xargs) actually says this about exiting,

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens.

One possible solution would, therefore, be to change exec_script to return exit status 255 on error.

Another possible solution, in the case that exec_script cannot be changed, would be to turn the plain xargs into a shell loop:

find "$script_dir/build/scripts" -name '*.sh' -print0 |
    sort -z |
    while IFS= read -r -d '' item && exec_script _ "$item"; do :; done

Here the loop will break if exec_script returns any non-zero exit value.

Yet another solution, this one from the comments and which arguably is the simplest external fix, is to catch any exit error from your script and replace it with 255:

find "$script_dir/build/scripts" -name '*.sh' -print0 |
    sort -z |
    xargs -r -0 -I% -n1 bash -c 'exec_script "$@" || exit 255' _ %

The documentation for xargs (see man xargs) actually says this about exiting,

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens.

One possible solution would, therefore, be to change exec_script to return exit status 255 on error.

Another possible solution, in the case that exec_script cannot be changed, would be to turn the plain xargs into a shell loop:

find "$script_dir/build/scripts" -name '*.sh' -print0 |
    sort -z |
    while IFS= read -r -d '' item && exec_script _ "$item"; do :; done

Here the loop will break if exec_script returns any non-zero exit value.

The documentation for xargs (see man xargs) actually says this about exiting,

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens.

One possible solution would, therefore, be to change exec_script to return exit status 255 on error.

Another possible solution, in the case that exec_script cannot be changed, would be to turn the plain xargs into a shell loop:

find "$script_dir/build/scripts" -name '*.sh' -print0 |
    sort -z |
    while IFS= read -r -d '' item && exec_script _ "$item"; do :; done

Here the loop will break if exec_script returns any non-zero exit value.

Yet another solution, this one from the comments and which arguably is the simplest external fix, is to catch any exit error from your script and replace it with 255:

find "$script_dir/build/scripts" -name '*.sh' -print0 |
    sort -z |
    xargs -r -0 -I% -n1 bash -c 'exec_script "$@" || exit 255' _ %
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

The documentation for xargs (see man xargs) actually says this about exiting,

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens.

One possible solution would, therefore, be to change exec_script to return exit status 255 on error.

Another possible solution, in the case that exec_script cannot be changed, would be to turn the plain xargs into a shell loop:

find "$script_dir/build/scripts" -name '*.sh' -print0 |
    sort -z |
    while IFS= read -r -d '' item && exec_script _ "$item"; do :; done

Here the loop will break if exec_script returns any non-zero exit value.