0

I tried to write a little bash code to write a temperaturelog. The into the logging file 100 timepoints and measuring points are written, then the data is moved to a temporary file and it should begin to record again into the original file. The temporary file is removed every 100 seconds because I only need the last minutes of record and want to prevent garbage.

Besides that the code might look unnecessarily complicated (I am a beginner) - Where is the mistake? I expected the counter (printed just to see what is going on) would count to 100 but it only prints out:

1
2 

and it writes only two timepoints instead of 100 into the files. Here is the code:

#!/bin/bash

COUNTER=0
#Initial temporary file is created
echo '' > temperaturelogtemporary.txt;

#100 timepoints are written into temperaturelog.txt   
while true; do
    echo `date` '->' `acpi -t`>> temperaturelog.txt;
    sleep 1;
    #as soon as 100 timepoints are recorded...      
    if [[ $COUNTER > 100 ]];
        then
                    #...the old temporary file is removed and 
                    #the last records are renamed into a new temporary file 
            rm temperaturelogtemporary.txt;
            mv temperaturelog.txt temperaturelogtemporary.txt;
            COUNTER=0;
    fi
    COUNTER=$(($COUNTER + 1));
    echo $COUNTER;

done
0

2 Answers 2

2

Referring to the accepted answer: while

[[ $COUNTER -ge 100 ]]

works, I still would recommend to use the equivalent

((COUNTER >= 100))

instead, because it is more readable.

Sign up to request clarification or add additional context in comments.

2 Comments

when exactly does one use the semicolons? They are still needed right?
You need them only if you put multiple statements on one line. In your script, you can strip most of them.
1

Just change ">" sign for "-ge".

if [[ $COUNTER -ge 100 ]];

Bash language is very very old - string and numeric comparisons performed with different keywords.

1 Comment

Note that -ge is >=, not >.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.