adding logact

This commit is contained in:
tengel 2024-03-20 11:28:46 -05:00
parent 6d0a582831
commit fdbddaadea

33
shell/logact.sh Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
#
# SPDX-License-Identifier: MIT
# Action logfile
ACTLOG="/tmp/test_logact.log"
[ -f "${ACTLOG}" ] && rm "${ACTLOG}"
# Run action, log output, return exit code
# - passing in 'sed' should be avoided
# - functions can only return 0..254
# -- set a global to check as needed
_ACTRET=0
function logact() {
local ACTION
ACTION="$*"
${ACTION} 2>&1 | tee -a "${ACTLOG}"
_ACTRET=${PIPESTATUS[0]}
# shellcheck disable=SC2086
return ${_ACTRET}
}
# Zero return test
logact echo "grep -l localhost /etc/hosts"
logact grep -l localhost /etc/hosts
logact echo -e "Direct: $? Global: $_ACTRET\n"
# Non-zero return test
logact echo "grep -l ZlocalhostZ /etc/hosts"
logact grep -l ZlocalhostZ /etc/hosts
logact echo -e "Direct: $? Global: $_ACTRET\n"
exit 0