Last active
October 21, 2020 17:27
-
-
Save cardil/333509d69d7760014827e2a62250590c to your computer and use it in GitHub Desktop.
Invalid & convoluted Bash scripting fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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