Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

An `awk` one-liner works on your RHEL box but fails on Ubuntu with `awk: line 1: function gensub never defined`.

IntermediateTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → gensub() (also asort(), PROCINFO, true multidimensional arrays) is a gawk extension. RHEL's awk is gawk; Ubuntu and Debian default to mawk, and Alpine uses BusyBox awk.

Diagnose →

readlink -f "$(command -v awk)"        # /usr/bin/mawk
awk -W version 2>/dev/null | head -1   # mawk prints its version this way

Fix → Use POSIX functions (sub, gsub, match, substr), or call gawk explicitly and declare it as a dependency:

# gensub(/([0-9]+)ms/, "\\1", "g") rewritten portably:
awk '{ gsub(/ms/, "", $5); print $5 }' access.log
command -v gawk >/dev/null || die "gawk is required (apt-get install gawk)"

Prevent → Write POSIX awk unless the script checks for gawk up front; test in the target distro's container.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment