0

私はbashを学んでいますが、今ではこのスクリプトを機能させるのに多くの問題を抱えています:

#!/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

このスクリプトを実行すると発生するエラーは次のとおりです。

./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

cat私は読んで周りを見回しており、出力が正しくBATTERYに割り当てられていると思います。のようないくつかの異なるものを試しまし{ [ ¨たが、何も機能しません。はい、ファイルは存在し、スクリプトと同じフォルダーにあります。

何かアドバイス?

4

3 に答える 3

3
BATTERY='cat battery.txt'

それは実行されませんcat battery.txt。「cat battery.txt」を文字列としてその変数に保存するだけです。

あなたがすべき:

BATTERY=$(cat battery.txt)

また

BATTERY=`cat battery.txt`

(最初の形式が優先されます。)

テストにも構文エラーがあります。たとえば、次のように使用します。

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

[[[実際にはまったく別のものです。

[testプログラム (check out ls /usr/bin/[and man test)、[[ expr ]]はシェル複合コマンド ( conditional expression ) です。

于 2012-09-28T08:19:44.910 に答える
0

条件の正しい構文は次のとおりです。

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

つまり、単一の角かっこはありません。数値比較には、二重括弧を使用することもできます。

if (( BATTERY==55 && BATTERY > 40 && D == 0 )) ; then ...
于 2012-09-28T08:20:54.243 に答える
0

コマンドの出力を取得するには、コマンドを引用符ではなくバッククォートで囲む必要があります。

BATTERY=`cat battery.txt`
        ^               ^
于 2012-09-28T08:19:43.037 に答える