Debian Codegraph @

assert.sh

raw symbols SCIP
1 #!/bin/bash
2 # assert.sh
3
4 #######################################################################
5 assert () # If condition false,
6 { #+ exit from script
7 #+ with appropriate error message.
8 E_PARAM_ERR=98
9 E_ASSERT_FAILED=99
10
11
12 if [ -z "$2" ] # Not enough parameters passed
13 then #+ to assert() function.
14 return $E_PARAM_ERR # No damage done.
15 fi
16
17 lineno=$2
18
19 if [ ! $1 ]
20 then
21 echo "Assertion failed: \"$1\""
22 echo "File \"$0\", line $lineno" # Give name of file and line number.
23 exit $E_ASSERT_FAILED
24 # else
25 # return
26 # and continue executing the script.
27 fi
28 } # Insert a similar assert() function into a script you need to debug.
29 #######################################################################
30
31
32 a=5
33 b=4
34 condition="$a -lt $b" # Error message and exit from script.
35 # Try setting "condition" to something else
36 #+ and see what happens.
37
38 assert "$condition" $LINENO
39 # The remainder of the script executes only if the "assert" does not fail.
40
41
42 # Some commands.
43 # Some more commands . . .
44 echo "This statement echoes only if the \"assert\" does not fail."
45 # . . .
46 # More commands . . .
47
48 exit $?