This is a
variation on a Linux challenge I found elsewhere on the Internet. I created a
file of numbers as follows:
Red Hat >cat file_of_numbers
134
Red Hat >cat file_of_numbers
134
065
258
168
Red Hat >
I wrote the following simple script to add the numbers, hoping that the result would be 625:
Red Hat >cat count_total.bash
I wrote the following simple script to add the numbers, hoping that the result would be 625:
Red Hat >cat count_total.bash
#!/bin/bash
total=0
for x in `cat
file_of_numbers`
do
((total+=$x))
done
echo "Total =
$total"
Red Hat >
However, when I ran it the result was slightly different:
Red Hat >./count_total.bash
However, when I ran it the result was slightly different:
Red Hat >./count_total.bash
Total = 613
Red Hat >
That was because the leading zero in 065 told Linux to treat it as octal which equals 6x8+5 = 53 in decimal.
I removed the leading zero:
Red Hat >cat file_of_numbers
That was because the leading zero in 065 told Linux to treat it as octal which equals 6x8+5 = 53 in decimal.
I removed the leading zero:
Red Hat >cat file_of_numbers
134
65
258
168
Red Hat >
…then Linux gave me the answer I wanted:
Red Hat >./count_total.bash
…then Linux gave me the answer I wanted:
Red Hat >./count_total.bash
Total = 625
Red Hat >