1

In one of my bash-scripts where I use a file to write the current system time, I noticed that the correct time only gets written to the file every second time the script runs.

I did a second script to test it, and it shows the same behavior.

The script:

#!/bin/bash
NOW=$(date +"%F_%H:%M:%S")
wtimestamp="/home/pazau/logfiles/overviewer-timestamp"
timestamp=$(<$wtimestamp)

echo $NOW > $wtimestamp

echo $timestamp

The system is Ubuntu 14.04 x64, and a Raspberry Pi model B with raspbian. The behavior is the same on both systems.

Anything i'm missing?

  • For me it seems to work correctly. Maybe you expect to see the script output $NOW instead of the previous contents of the file: $timestamp. – agold Feb 19 '16 at 8:20
  • 3
    What do you mean by "correct time only gets written to the file every second time"? The other times the file is not written? Or the other times an incorrect time is written? – techraf Feb 19 '16 at 8:27
  • @agold Nope, I was my intention to show the content from the file, to ensure that the time was correct written. – Nicolas Ø Feb 19 '16 at 12:00
  • @techraf As far as I could see, the timestamp in the file was incorrect. As it turns out, the error happened because of wrong ordering. – Nicolas Ø Feb 19 '16 at 12:02
2

The time stored is already correct, you just need to reorder the instructions to get what you expect:

#!/bin/bash
NOW=$(date +"%F_%H:%M:%S")
wtimestamp="/home/pazau/logfiles/overviewer-timestamp"
echo $NOW > $wtimestamp
timestamp=$(<$wtimestamp)
echo $timestamp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.