Skip to content

Instantly share code, notes, and snippets.

@cardil
Last active October 21, 2020 17: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 cardil/333509d69d7760014827e2a62250590c to your computer and use it in GitHub Desktop.
Save cardil/333509d69d7760014827e2a62250590c to your computer and use it in GitHub Desktop.
Invalid & convoluted Bash scripting fixed
#!/usr/bin/env bash
set -Eeuo pipefail
function testing.of() {
local what
what="${1:?Pass what as arg[1]}"
echo "Testing of ${what}..."
eval "non-existing-showcase-command ${what}"
}
function do.aaa() {
echo 'Doing Aaa...'
if echo "${SCOPE:-}" | grep -q aaa; then
testing.of aaa || return $?
fi
echo 'Done Aaa.'
}
function do.bbb() {
echo 'Doing Bbb...'
if echo "${SCOPE:-}" | grep -q bbb; then
testing.of bbb || return $?
fi
echo 'Done Bbb.'
}
failed=0
(( !failed )) && do.aaa || failed=1
(( !failed )) && do.bbb || failed=2
(( failed )) && exit $failed
echo 'SUCCESS!'
#!/usr/bin/env bash
set -Eeuo pipefail
function testing.of() {
local what
what="${1:?Pass what as arg[1]}"
echo "Testing of ${what}..."
eval "non-existing-showcase-command ${what}"
}
function do.aaa() {
echo 'Doing Aaa...'
if echo "${SCOPE:-}" | grep -q aaa; then
testing.of aaa
fi
echo 'Done Aaa.'
}
function do.bbb() {
echo 'Doing Bbb...'
if echo "${SCOPE:-}" | grep -q bbb; then
testing.of bbb
fi
echo 'Done Bbb.'
}
failed=0
(( !failed )) && do.aaa || failed=1
(( !failed )) && do.bbb || failed=2
(( failed )) && exit $failed
echo 'SUCCESS!'
#!/usr/bin/env bash
set -Eeuo pipefail
function stacktrace() {
local code="${1:-$?}"
set +o xtrace
echo -e "\n\033[1;31mFAIL! Exiting with status ${code}!\033[0m\n"
if [ ${#FUNCNAME[@]} -gt 2 ]
then
echo "Call tree:"
echo " 0: ${BASH_SOURCE[1]}:${BASH_LINENO[0]} ${BASH_COMMAND}"
for ((i=1;i<${#FUNCNAME[@]}-1;i++))
do
echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
done
fi
exit "${code}"
}
trap stacktrace ERR
function testing.of() {
local what
what="${1:?Pass what as arg[1]}"
echo "Testing of ${what}..."
eval "non-existing-showcase-command ${what}"
}
function do.aaa() {
echo 'Doing Aaa...'
if echo "${SCOPE:-}" | grep -q aaa; then
testing.of aaa
fi
echo 'Done Aaa.'
}
function do.bbb() {
echo 'Doing Bbb...'
if echo "${SCOPE:-}" | grep -q bbb; then
testing.of bbb
fi
echo 'Done Bbb.'
}
do.aaa
do.bbb
echo 'SUCCESS!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment