2014-03-02 38 views
-1

聲明當我運行一個名爲anom一個C程序,它在腳本返回01比較整數值,如果在bash

while : 
do 
xentop -b -d 1 -i 50 >> out 
cpu=$(tail -n 50 out| tr '\r' '\n' | col -bx | awk '{ total += $4 } END { print total/50 }') 
echo "$cpu" 
echo "$cpu_before" 
det=$(./anom $cpu $cpu_before) 
cpu_before=$cpu; 
echo "$det" 
if [ "$det" -eq "1" ];then 
    echo "Detected" 
    c=c+1 
fi 
done 

它返回error線路if [ "$det" -eq "1" ];then

[: : integer expression expected 

我試過這樣:

if [ "$det" -eq 1 ];then 

但我得到相同的錯誤。

+0

它*返回*,或它*輸出*? –

+0

從「$ det」中刪除「」,成爲一個字符串,-eq期望整數。檢查人測試 –

+0

@ Ignacio Vazquez-Abrams:它返回。 – Mjina

回答

0

如果你在$ DET正確的價值,

你爲什麼不嘗試

if [[ $det -eq 1 ]] 
then 
    echo "Detected" 
    c=$(($c + 1)) 
fi 

的一件事錯了,我在你的代碼中看到的是,要轉換的值成在比較之前的字符串..和字符串我們使用=運算符進行比較。

+0

這只是複製粘貼,沒有真正看到有什麼..我的壞.. –

+1

**在bash中的所有標量是字符串。它是*運算符*,用於決定值如何被'['解釋。 –

+0

@Nishant Shrivastava:它工作,你只是忘了「 - 」爲「-eq」 – Mjina

1

上一條命令的返回值存儲在$?中。此外,諸如ifwhile之類的命令會檢查調用真值(0)的命令的返回值,從而允許您直接使用它們。

if ! ./anom "$cpu" "$cpu_before" ; then 
    ... 
fi 
cpu_before="$cpu"