I'm having this weird issue that I've been staring at for a few hours now and I can't seem to figure out whats wrong. I'm writing a dependency management system for a larger project and one of the front-end scripts, which actually does little more than just parsing CLI arguments and calling a library function with the correct options, consistently gets terminated by the OS with a SIGTERM signal (the ERR
trap) seemingly due to an error that gets raised by or around the pipe to xargs
, hence exiting with code 143.
The offending exerpt:
set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit nullglob dotglob globstar
trap 'kill 0' ERR
(return 0 2>/dev/null) \
&& echo 'This script cannot be sourced' >&2 \
&& exit 1
# Built-in default options
declare -A ARGS_DEFAULTS+=(
[MODULES]=
[PATHNAMES]=
[DEPENDENCIES]=
[REVERSE]=
)
# ------------------------------------------------------
# Variable setup and argument parsing goes here, omitted
# for brevity. Assume all variables have correct values
# (tested). All arguments are ultimately saved in the
# `ARGS` associative array. Options from boolean CLI
# arguments (e.g. `ARGS[PATHNAMES]`) will hold `1` if
# set, `0` or `null` otherwise, depending on whether or
# not it has a user-defined default value.
# ------------------------------------------------------
if (( ${ARGS[DEPENDENCIES]:-0} ))
then if (( ${ARGS[REVERSE]:-0} ))
then @module:dependents "$MODS_DIR/" "${ARGS[MODULES]}"
else @module:dependencies "$MODS_DIR/" "${ARGS[MODULES]}"
fi
else @module:resolve "$MODS_DIR/" "${ARGS[MODULES]}"
# Replacing `xargs` with other commands — e.g. `head` or
# `tail` — results in the same error.
fi | xargs -I{} echo ${ARGS[PATHNAMES]:+"$MODS_DIR/"}'{}'
The script outputs the correct information, but gets terminated upon reaching the pipe to xargs
. Of course, I can write the exact same in the terminal and it works just fine, even with the pipefail option set. The xargs
command doesn't seem related (at least not directly). For instance, if substituted with any other command (head
or tail
, for example), the same problem happens. Both ARGS
and MODS_DIR
variables are defined, as would any one of them not be correctly defined and initialized, execution of the script would result in no valid output at all.
Any ideas?
in this case, the error disappeared mysteriously while adding and removing test code. Both the snippet posted here and the corresponding part of the script on my computer diff identical. My best guess is that there must have been some junk Unicode character that either displayed as a space, or not at all, and that made the script choke, and in the doing and undoing, I may have at some point added a space or a newline manually that overrode the junk character. Or perhaps the editor got rid of it upon undoing.
In case it helps someone in a similar situation, might be worth trying downgrading the character encoding to plain 8-bit ASCII and see if that fixes the problem, or (if using UTF8) use a byte reader utility like xxd
and check for strange multi-byte patterns.