0

I have the following bash shell script which 90% of the time does the expected thing, but sometimes it doesn't. The problem is with the placement of the date and time. As shown, I have told the program to append the date at the end of screenlog.0. Most of the time, this happens properly. However, sometimes the date and time get appended to the beginning of screenlog.0. This is really troublesome as the format of screenlog.0 needs to adhere to certain guidelines. Any help would be appreciated.

#!/bin/bash
...
...

if grep -q "dicounter_${string1}_from_${string2}" MasterFile.txt || [ -e "dicounter_${string1}_from_${string2}" ];
then
   echo "dicounter_${string1}_from_${string2} already exists in the MasterFile or the current directory. Would you like to proceed?"
   read string3
   if [[ "${string3^}" == 'Y' ]]; then
      echo "How many times has this dicounter been retested? (e.g. 1 for first time, 2 for the second, ...)"
      read string4
      screen -S trans -L /dev/ttyACM0
      screen -S trans -X stuff 's'$(echo -ne '\015')
      sleep 8s
      screen -S trans -X quit
      date +%F~%H:%M:%S >> screenlog.0
      mv screenlog.0 dicounter_${string1}_from_${string2}~${string4}

   else
      exit 0
   fi
else
  #opening screen & begin analysis
  screen -S trans -L /dev/ttyACM0
  screen -S trans -X stuff 's'$(echo -ne '\015')
  sleep 8s
  screen -S trans -X quit
  date +%F~%H:%M:%S >> screenlog.0
  mv screenlog.0 dicounter_${string1}_from_${string2}

fi

Essentially, the program is to look to see if the dicounter_..._from_... already exists, and if not, or if the user wants another test run, proceed and connect to a transmitter and take data. At the end, a screenlog.0 file is created, to which I want to append the date and time at the end of the file. Then rename it.

  • you're appending the output, so my only theory is that screen hasn't written the file when you call date, and so date creates a new file. The only workaround I can think of would be adding sleeps in-between, or perhaps a busy-loop checking for a non-zero size of screenlog.0 – Jeff Schaller Jun 2 '17 at 15:52
  • Or just add a ;date to the command you're stuffing into the screen session. – DopeGhoti Jun 2 '17 at 15:54
  • Yes, it was the premature appending that was the issue. I added sleep 2s and for now its all good. – Ptheguy Jun 2 '17 at 15:58
  • If any of the answers solved your problem, please consider (voting) and clicking the checkmark to accept it. Thank you! – Jeff Schaller Jun 11 '17 at 12:15
1

It appears that the screen "quit" command was returning before flushing the log. Adding a delay between the final screen command and the datestamp should allow time for screen to finish writing to the log. Another option would be to use a "busy" loop to wait for the log to be written to:

...
screen -S trans -X quit
while [ ! -s screenlog.0 ]
do
  sleep 1
done
date +%F~%H:%M:%S >> screenlog.0
...

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.