Linky
Linky

Reputation: 11

While file exists in bash

Pretty much it checks if the file 'ok.temp' exists. If it doesn't it goes into a loop of sleeping for 2 seconds before checking again. After the file is deemed that it exists it continues on.

Unfortunately I'm getting the errors stated below the following code:

#!/bin/sh
# Read the sensor values
if [ ! -f saved.txt ]
then
    touch saved.txt
fi 

#saved values count
sCount=0

#value aggregates
vTTemp=0
vTHumid=0
vTLux=0

#previous aggregates
pTTemp=0
pTHumid=0
pTLux=0

while : 
do
vTemp=0
vHumid=0
vLux=0

if [ $sCount -gt 0 ] 
then
    if [ $(( $sCount % 5 )) -eq 0 ]
    then
        #set previous aggregates as current
        pTTemp=$vTTemp
        pTHumid=$vTHumid
        pTLux=$vTLux

        #reset current aggregates
        vTTemp=0
        vTHumid=0
        vTLux=0
    fi
fi

    while [ ! –f ok.temp ]
    do
        sleep 20
    done

    if [ -f logfile.txt ]
    then
        rResult=`tail -1 logfile.txt`
        rSaved=`tail -1 saved.txt`
        if [ $rResult -eq $rSaved ]
        then
            vTemp=`echo $rResult | cut -d" " -f1`
            vHumid=`echo $rResult | cut -d" " -f2`
            vLux=`echo $rResult | cut -d" " -f3`
            echo $rResult >> saved.txt

            # aggregate results
            vTTemp=`expr $vTTemp + $vTemp`
            vTHumid=`expr $vTHumid + $vHumid`
            vTLux=`expr $vTLux + $vLux`
            echo 'Most recent results recieved from sensor has been saved'
        else
            echo 'Most recent result has already been saved, skipping'
        fi
    fi

    echo '[Previous] Temp:'$pTTemp' Humid:'$pTHumid' Lux:'$pTLux
    echo '[Current] Temp:'$vTTemp' Humid:'$vTHumid' Lux:'$vTLux

    sCount=`expr $sCount + 1`
done

And I'm getting these errors:

Line 43 = while [ ! –f ok.temp ]

line 43: [: f: unary operator expected

Line 52 = if [ $rResult -eq $rSaved ]

line 52: [: too many arguments

Any help would be greatly appreciated :)

Upvotes: 1

Views: 3476

Answers (4)

l0b0
l0b0

Reputation: 58788

That's not a dash in line 43 - It's an en dash (copy it and search on that page to see it). Did you copy that code from a blog? They often do such replacements where they shouldn't. The code should read:

while [ ! -f ok.temp ]

-eq is for numbers, not strings - This should do the trick (the quotes are important):

if [ "$rResult" = "$rSaved" ]

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

One of your variables is empty, or contains multiple words. Use quotes:

if [ "$rResult" = "$rSaved" ]

Upvotes: 0

Summer_More_More_Tea
Summer_More_More_Tea

Reputation: 13356

It is probable that some variable is not set or empty in the script. You can set set -x to turn on debug mode.

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 359965

Most likely it's because you're using unquoted variables that are empty inside single square brackets. You can fix this by using the preferred double square bracket form or by quoting the variables (doing both isn't necessary).

You have tagged your question , but your shebang says #!/bin/sh and the form of your script follows Bourne shell conventions instead of using Bash-specific features. If you'd like, I can comment on how you can take advantage of those Bash features.

Upvotes: 0

Related Questions