#!/usr/bin/env bash # exit if a command fails set -o errexit # display last non-zero exit code in a failed pipeline set -o pipefail # subshells and functions inherit ERR traps set -E # set core count for thread limit CORE_COUNT="$(nproc)" # create tempfile TEMP_FILE="$(mktemp)" # remove the tempfile on exit function cleanup { rm -rf "${TEMP_FILE}" } trap cleanup EXIT # write opening tags of output file cat <<'EOL' > "${TEMP_FILE}" HTTP/1.1 200 Everything Is Just Fine Server: netcat v1.10-41.1 Content-Type: text/html; charset=UTF-8
EOL

# if a pipe exists on stdin, use that as the output
if [ -p /dev/stdin ] && [ "$#" -eq 0 ]; then
  cat >> "${TEMP_FILE}"
# otherwise accept a single argument
elif [ ! -p /dev/stdin ] && [ "$#" -eq 1 ]; then
  echo "$1" >> "${TEMP_FILE}"
else
  echo -e "\\nUsage: $0  OR  | $0\\n"
  exit 1
fi

# write closing tags of output file
cat <<'EOL' >> "${TEMP_FILE}"
EOL # display contents of tempfile, allowing up to 8 jobs at once while : do { nc -z -l -p1500 -q1 -w1 < "${TEMP_FILE}" & } 2>/dev/null if [ "$(jobs -r | wc -l)" -ge "${CORE_COUNT}" ]; then sleep 1 fi done