Debian Codegraph @

bad-op.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # bad-op.sh: Trying to use a string comparison on integers.
3
4 echoscip-shell system . . echo.
5 number=1
6
7 # The following while-loop has two errors:
8 #+ one blatant, and the other subtle.
9
10 while [scip-shell system . . `[`. "$number" < 5 ] # Wrong! Should be: while [ "$number" -lt 5 ]
11 do
12 echoscip-shell system . . echo. -n "$number "
13 let "number += 1"
14 done
15 # Attempt to run this bombs with the error message:
16 #+ bad-op.sh: line 10: 5: No such file or directory
17 # Within single brackets, "<" must be escaped,
18 #+ and even then, it's still wrong for comparing integers.
19
20 echoscip-shell system . . echo. "---------------------"
21
22 while [scip-shell system . . `[`. "$number" \< 5 ] # 1 2 3 4
23 do #
24 echoscip-shell system . . echo. -n "$number " # It *seems* to work, but . . .
25 let "number += 1" #+ it actually does an ASCII comparison,
26 done #+ rather than a numerical one.
27
28 echoscip-shell system . . echo.; echoscip-shell system . . echo. "---------------------"
29
30 # This can cause problems. For example:
31
32 lesser=5
33 greater=105
34
35 if [scip-shell system . . `[`. "$greater" \< "$lesser" ]
36 then
37 echoscip-shell system . . echo. "$greater is less than $lesser"
38 fi # 105 is less than 5
39 # In fact, "105" actually is less than "5"
40 #+ in a string comparison (ASCII sort order).
41
42 echoscip-shell system . . echo.
43
44 exit 0