Created
April 6, 2016 09:40
-
-
Save SmetDenis/fd25635d0a6a4e618c5e7a7f085b06ee to your computer and use it in GitHub Desktop.
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 sh | |
####################################################################### | |
assert () # If condition false, | |
{ #+ exit from script | |
#+ with appropriate error message. | |
E_PARAM_ERR=98 | |
E_ASSERT_FAILED=99 | |
if [ -z "$2" ] # Not enough parameters passed | |
then #+ to assert() function. | |
return $E_PARAM_ERR # No damage done. | |
fi | |
lineno=$2 | |
if [ ! $1 ] | |
then | |
echo "Assertion failed: \"$1\"" | |
echo "File \"$0\", line $lineno" # Give name of file and line number. | |
exit $E_ASSERT_FAILED | |
# else | |
# return | |
# and continue executing the script. | |
fi | |
} # Insert a similar assert() function into a script you need to debug. | |
####################################################################### | |
a=5 | |
b=4 | |
condition="$a -lt $b" # Error message and exit from script. | |
# Try setting "condition" to something else | |
#+ and see what happens. | |
assert "$condition" $LINENO | |
a=4 | |
b=5 | |
condition="$a -lt $b" # Error message and exit from script. | |
# Try setting "condition" to something else | |
#+ and see what happens. | |
assert "$condition" $LINENO | |
# The remainder of the script executes only if the "assert" does not fail. | |
# Some commands. | |
# Some more commands . . . | |
echo "This statement echoes only if the \"assert\" does not fail." | |
# . . . | |
# More commands . . . | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment