0

I'm learning bash but now I'm having a lot of trouble making this script work:

#!/bin/bash

A="0"
B="0"
C="0"
D="0"
E="0"
F="0"
G="0"

while true; do

sleep 1

BATTERY='cat battery.txt'

if [["$BATTERY" -le 100] && ["$BATTERY" -gt 85] && [$A -eq 0]]; then

A="1"
    commands... 

elif [["$BATTERY" -le 85] && ["$BATTERY" -gt 70] && [$B -eq 0]]; then

B="1"
    commands...

elif [["$BATTERY" -le 70] && ["$BATTERY" -gt 55] && [$C -eq 0]]; then

C="1"
    commands...

elif [["$BATTERY" -le 55] && ["$BATTERY" -gt 40] && [$D -eq 0]]; then

D="1"
commands...

elif [["$BATTERY" -le 40] && ["$BATTERY" -gt 25] && [$E -eq 0]]; then

E="1"   
    commands...

elif [["$BATTERY" -le 25] && ["$BATTERY" -gt 10] && [$F -eq 0]]; then

F="1"
commands...

elif [["$BATTERY" -le 10] && ["$BATTERY" -gt 0] && [$G -eq 0]]; then

G="1"
commands...
fi
done

The error that I get when I execute this script is:

./changewill.sh: line 17: [[cat battery.txt: command not found
./changewill.sh: line 27: [[cat battery.txt: command not found
./changewill.sh: line 36: [[cat battery.txt: command not found
./changewill.sh: line 45: [[cat battery.txt: command not found
./changewill.sh: line 54: [[cat battery.txt: command not found
./changewill.sh: line 63: [[cat battery.txt: command not found
./changewill.sh: line 72: [[cat battery.txt: command not found

I have been reading and looking around and I think the cat output is correctly assigned to BATTERY. I tried with some different things like { [ ¨, but nothing works. And yes, the file exists and it's in the same folder with the script.

Any advice?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
HumiD
  • 35
  • 1
  • 1
  • 5
  • Are you running the script in the script's directory or having it in the path? – BugFinder Sep 28 '12 at 08:16
  • Try [this](http://stackoverflow.com/a/4139214/645270). – keyser Sep 28 '12 at 08:18
  • Both `[[` and `[` need to be surrounded with spaces -- and see the [bash manual](http://www.gnu.org/software/bash/manual/html_node/index.html) for more information on [`[`](http://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins) (look for "test") and [`[[`](http://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs). – Keith Thompson Sep 28 '12 at 09:31

3 Answers3

3
BATTERY='cat battery.txt'

That doesn't execute cat battery.txt, it just saves "cat battery.txt" as a string into that variable.

You should:

BATTERY=$(cat battery.txt)

or

BATTERY=`cat battery.txt`

(First form is preferred.)

Your tests have syntax errors too. Use for example:

elif [[ $BATTERY -le 10 && $BATTERY -gt 0 && $G -eq 0 ]]; then ...

[ and [[ are actually completely different things.

[ is the test program (check out ls /usr/bin/[ and man test), [[ expr ]] is a shell compound command (conditional expression).

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Why is the first form is better than the second? – Blender Sep 28 '12 at 08:20
  • It's easier to use if you need to quote things in the thing you're executing, or if you need to nest them. – Mat Sep 28 '12 at 08:21
  • I think this solved the problem with cat, but I'm still having this error: ./changewill.sh: line 17: [[30: command not found ./changewill.sh: line 27: [[30: command not found ./changewill.sh: line 36: [[30: command not found ./changewill.sh: line 45: [[30: command not found ./changewill.sh: line 54: [[30: command not found ./changewill.sh: line 63: [[30: command not found ./changewill.sh: line 72: [[30: command not found – HumiD Sep 28 '12 at 08:51
  • 1
    You need a space after `[` . Actually your tests all look real odd. – Mat Sep 28 '12 at 08:52
0

To get the output of a command, you need to wrap the command with backticks, not quotes:

BATTERY=`cat battery.txt`
        ^               ^
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Blender
  • 289,723
  • 53
  • 439
  • 496
0

The correct syntax for conditions is

[[ $BATTERY -le 55 && $BATTERY -gt 40 && $D -eq 0 ]]

i.e. no single square brackets. For numeric comparison, you can use the double parentheses, too:

if (( BATTERY==55 && BATTERY > 40 && D == 0 )) ; then ...
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Thanks, that was the problem! – HumiD Sep 28 '12 at 09:15
  • Single square brackets are also acceptable. `[` ... `]` and `[[` ... `]]` have different semantics; see the bash documentation for details. One of the problems in the code in the question is the lack of spaces around the square brackets. – Keith Thompson Sep 28 '12 at 09:25
  • This fails to address the problem that `BATTERY` is just a static piece of text, not (as was apparently intended) the contents of a file. The value of the variable is literally `cat battery.txt`. – tripleee Sep 28 '12 at 10:57