0

I know I'm missing something very obvious, but I just can't see it. In the code below, new_i variable gets incremented properly, however, when I get to the if statement, I think my syntax is off. I looked at some examples, but none show variable set to zero when if statement is greater than (in this case 15) a number.

#!/bin/sh
i=$(cat /etc/hour.conf)
new_i=$((i+1))
if [[ "$new_i" -gt 15 ]]; then
 new_i=0
fi
echo "$new_i">/etc/hour.conf
echo "$new_i"

When I run this script I get the following error:

./loops: 3: ./loops: Illegal number: new_i

Thanks in advance for your help!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
user3308131
  • 159
  • 1
  • 5
  • 16
  • Remove space after `=`: `new_i=0` – grundic Jun 16 '17 at 14:22
  • that gives the same error message – user3308131 Jun 16 '17 at 14:23
  • Ah, the problem is on line **3**... What is content of `/etc/hour.conf`? – grundic Jun 16 '17 at 14:27
  • echo 0>/etc/hour.conf – user3308131 Jun 16 '17 at 14:28
  • Hm.. Can you cat that file? `echo 0>x.txt && cat x.txt` gives me empty file. To fix it you have to add space before `>`: **echo 0 >/etc/hour.conf** – grundic Jun 16 '17 at 14:30
  • @user3308131, why do you think `new_i` is greater than 15? The code is syntactically correct. Try to replace `i=$(cat /etc/hour.conf)` with `i=4`, for instance – Ruslan Osmanov Jun 16 '17 at 14:31
  • Apart from the space in the assignment you had earlier, that seems to work ok. As long as the file contains a number, and not for example the string `new_i`. Which you could get if the echo to the file was typoed as `echo "new_i" > /etc/hour.conf`. But not from Bash. It would take the string as a variable name (yes, really, try `bash -c 'y=123; x=y; echo $(( x+1 ))' `. But then your hashbang line doesn't say `bash`, it says `/bin/sh`. – ilkkachu Jun 16 '17 at 14:37
  • @user3308131, you should fetch the number from file with sed, awk, perl etc. Enclose the command substitution in double quotes: `i="$(....)"` – Ruslan Osmanov Jun 16 '17 at 14:38
  • (And of course if your `/bin/sh` just so happens to be, say `dash`, it doesn't support `[[..]]`.) – ilkkachu Jun 16 '17 at 14:41
  • @RuslanOsmanov, what difference do you suppose `sed` would make compared to `cat` if the file only contains a number. And while double-quoting is a good habit, it doesn't do a difference in an assignment like that. – ilkkachu Jun 16 '17 at 14:42
  • @ilkkachu, how do you know that the file contains only a number? Yes, he can leave it as `i=$(...)`. – Ruslan Osmanov Jun 16 '17 at 14:45
  • The code I run is correct if I change the filename `/etc/hour.conf` to `x.txt` and the initial content of the file is empty. – zhenguoli Jun 16 '17 at 14:51
  • If you edit your code to remove bugs after they're pointed out, the result is a comment thread that makes no sense to new readers. – Charles Duffy Jun 16 '17 at 15:49

2 Answers2

2

This works - the $ in front of the i in the new_i=$(( line and the removal of the quotes and one set of the brackets fixed the errors and now the scripts works as intended. Thanks to everyone for their help!

#!/bin/sh
i=$(cat /etc/hour.conf)
new_i=$(($i+1))
if [ $new_i -gt 15 ]; then
 new_i=0
fi
echo "$new_i">/etc/hour.conf
echo "$new_i"
user3308131
  • 159
  • 1
  • 5
  • 16
0

Try without the quotes:

if [ $new_i -gt 15 ]; then
    ...
fi

or, better yet, use arithmetic evaluation instead:

if (( $new_i > 15 )) ; then
    ...
fi
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85