Debian Codegraph @

bad-op.sh

raw symbols SCIP
1 #!/bin/bash
2 # bad-op.sh: Trying to use a string comparison on integers.
3
4 echo
5 number=1
6
7 # The following while-loop has two errors:
8 #+ one blatant, and the other subtle.
9
10 while [ "$number" < 5 ] # Wrong! Should be: while [ "$number" -lt 5 ]
11 do
12 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 echo "---------------------"
21
22 while [ "$number" \< 5 ] # 1 2 3 4
23 do #
24 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 echo; echo "---------------------"
29
30 # This can cause problems. For example:
31
32 lesser=5
33 greater=105
34
35 if [ "$greater" \< "$lesser" ]
36 then
37 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 echo
43
44 exit 0