Skip to content

Instantly share code, notes, and snippets.

@vlad-vinogradov
Created May 23, 2020 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlad-vinogradov/d1b568d61bffea3bc2e4ef1e800b0a71 to your computer and use it in GitHub Desktop.
Save vlad-vinogradov/d1b568d61bffea3bc2e4ef1e800b0a71 to your computer and use it in GitHub Desktop.
This apt-get wrapper script (bash) is intended mainly for use with Dockerfile. It allows to detect apt-get network errors and to avoid distracting "red" apt-get output.
#!/usr/bin/env bash
#
# "apt-get" wrapper that detects STDERR messages with the prefixes "W:" and "E:"
# and redirect some apt-get STDERR messages to STDOUT.
#
# Returns exit status 120 in case of non-zero apt-get exit status
# or in case of detection of STDERR message with the prefix "W:" or "E:".
#
# The wrapper is intended mainly for use with Dockerfile.
# It allows to detect apt-get network errors
# and to avoid distracting "red" apt-get output.
#
# See also "Advanced Bash−Scripting Guide":
# Chapter 16. I/O Redirection
# Chapter 22. Process Substitution
# Chapter 11. Internal Commands and Builtins ('read' command)
declare -r MYEXECNAME="${BASH_SOURCE[0]##*/}"
declare -r MYSUCCESS='@@@zZ:^&?@%#:#%@?&^:Zz@@@'
declare -u myupper=
declare myok=
declare mystatus=
declare myerr=
declare myline=
exec 10>&1 # Link file descriptor #10 with STDOUT
while IFS= read -r myline
do
# Handle apt-get STDERR line
# (customize redirecting to STDOUT according to your needs)
if [ "$myline" = "$MYSUCCESS" ]; then
myok=1
elif [ -z "$myline" ]; then
:
elif [ "${myline#*Extracting templates from packages: 100%}" != "$myline" ]; then
echo 'Extracting templates from packages: 100%'
elif [ "${myline#debconf: delaying package configuration}" != "$myline" ]; then
echo "$myline"
else
echo "$myline" 1>&2
myupper="${myline:0:2}"
if [ "$myupper" = 'E:' -o "$myupper" = 'W:' ]; then
myerr=1
fi
fi
done < <(
# This is subshell environment ...
# Redirect apt-get STDERR to the pipe and STDOUT to the #10:
apt-get "$@" 2>&1 >&10
mystatus="$?"
echo # Terminate possible pipe line before echo "$MYSUCCESS"
if [ "$mystatus" = 0 ] ; then
echo "$MYSUCCESS"
else
echo "${MYEXECNAME}: apt-get" "$@" "- Exit Status ${mystatus}"
fi
)
exec 10>&- # Close file descriptor #10
if [ "$myok" != 1 ]; then
exit 120
elif [ "$myerr" = 1 ]; then
echo "FAILURE: ${MYEXECNAME}: apt-get error/warning message is detected" 1>&2
exit 120
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment