1

With a bash script, I am trying to verify that there is 15 seconds between file modifications. If I have:

-rw-rw-r--  1 root      root           2739 Jun 05 00:43 1370392620.log
-rw-rw-r--  1 root      root           2739 Jun 05 00:37 1370392623.log
-rw-rw-r--  1 root      root           2739 Jun 05 00:37 1370392626.log
-rw-rw-r--  1 root      root           2739 Jun 05 00:37 1370392630.log

I need to be able to get the seconds difference between these timestamps, and verify that they are 15 seconds apart.

4

1 回答 1

0
last=0
ls -rt *.log |
while read fname 
do
  epoch_seconds=$(date -r $fname +%s)
  if [ $last -gt 0 ]; then
    printf "%20s   diff: %10d   %13d\n" "$fname"  $(( $epoch_seconds - $last )) $epoch_seconds
    [  $(( $epoch_seconds - $last )) -ne 15 ]  && echo "file mtime difference error"
    last=$epoch_seconds     
  else
    printf "%20s   start %10d   %13d\n" "$fname" 0  $epoch_seconds
    last=$epoch_seconds
  fi

done

The date -f filename command (GNU) returns the mtime (last time written, i.e. closed time), formatted as +%s gives epoch seconds. Subtract first file mtime from next file, ls -rt sorts file by reverse mtime -i.e., oldest first, newest last.

于 2013-06-07T02:55:59.567 回答