1
0

yesterday 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/rc
  2. rfork e
  3. smon='s/Jan/01/
  4. s/Feb/02/
  5. s/Mar/03/
  6. s/Apr/04/
  7. s/May/05/
  8. s/Jun/06/
  9. s/Jul/07/
  10. s/Aug/08/
  11. s/Sep/09/
  12. s/Oct/10/
  13. s/Nov/11/
  14. s/Dec/12/'
  15. fn usage {
  16. echo 'usage: yesterday [-abcCdDs] [-[[[yy]yy]mm]dd] [-n daysago] file ...' >[1=2]
  17. exit 'usage'
  18. }
  19. fn Xcp {
  20. echo cp $1 $2
  21. cp $1 $2
  22. }
  23. fn Xdiff {
  24. echo diff $1 $2
  25. diff $1 $2
  26. }
  27. fn Xdiffn {
  28. echo diff -n $1 $2
  29. diff -n $1 $2
  30. }
  31. fn Xadiff {
  32. echo /acme/bin/adiff $1 $2
  33. /acme/bin/adiff $1 $2
  34. }
  35. fn Xcarefulcp {
  36. if(! cmp -s $1 $2) Xcp $1 $2
  37. }
  38. fn Xecho {
  39. echo $1
  40. }
  41. fn Xbind {
  42. echo bind $1 $2
  43. bind $1 $2
  44. }
  45. year=`{date|sed 's/.* //'}
  46. copy=Xecho
  47. last=()
  48. defdump=dump
  49. while(! ~ $#* 0 && ~ $1 -* && ! ~ $1 --){
  50. switch($1){
  51. case -a
  52. copy=Xadiff
  53. shift
  54. case -b
  55. copy=Xbind
  56. shift
  57. case -c
  58. copy=Xcp
  59. shift
  60. case -d
  61. copy=Xdiff
  62. shift
  63. case -D
  64. copy=Xdiffn
  65. shift
  66. case -C
  67. copy=Xcarefulcp
  68. shift
  69. case -s
  70. defdump=snap
  71. shift
  72. case -n*
  73. if(~ $1 -n){
  74. if(~ $#* 1)
  75. usage
  76. shift
  77. days=$1
  78. }
  79. if not
  80. days=`{echo $1 | sed 's/^-.//'}
  81. last=`{date `{hoc -e `{date -n} ^ '-'$days'*60*60*24'} | \
  82. sed -e 's%... (...) (..) ..:..:.. ... (....)%\3/\1\2%' -e 'y/ /0/' -e $smon}
  83. shift
  84. case -?
  85. mon=`{date|sed -e 's/^....(...).*/\1/' -e $smon}
  86. last=$year/$mon ^`{echo $1|sed 's/^-/0/'}
  87. shift
  88. case -??
  89. mon=`{date|sed -e 's/^....(...).*/\1/' -e $smon}
  90. last=$year/$mon ^`{echo $1|sed 's/^-//'}
  91. shift
  92. case -????
  93. last=$year/ ^ `{echo $1|sed 's/^-//'}
  94. shift
  95. case -??????
  96. last=`{echo $year|sed 's/..$//'} ^ `{echo $1|sed 's/^-(..)/\1\//'}
  97. shift
  98. case -????????
  99. last=`{echo $1|sed 's/^-(....)/\1\//'}
  100. shift
  101. case *
  102. usage
  103. }
  104. }
  105. if(! ~ $#* 0 && ~ $1 --)
  106. shift
  107. if(~ $#* 0)
  108. usage
  109. dir=`{pwd}
  110. if(! ~ $status ''){
  111. echo 'yesterday: can''t find directory' >[1=2]
  112. exit 'pwd failed'
  113. }
  114. for(i){
  115. xpath=`{cleanname -d $dir -- $i}
  116. xdump=$defdump
  117. dumppath=$xpath
  118. if(~ $xpath /n/*/*){
  119. xdump=`{echo $xpath | sed 's:/n/([^/]+)/.*:\1'$defdump':'}
  120. dumppath=`{echo $xpath | sed 's:/n/[^/]+(/.*):\1:'}
  121. }
  122. if(! test -e /n/$xdump/$year)
  123. 9fs $xdump
  124. if(~ $#last 0){
  125. xlast=`{ls /n/$xdump/$year|sed -n '$p'}
  126. switch($defdump){
  127. case snap
  128. xlast=`{ls $xlast|sed -n '$p'}
  129. }
  130. }
  131. if not
  132. xlast=/n/$xdump/$last
  133. if(! test -e $xlast){
  134. echo 'yesterday:' \
  135. `{echo $xlast|sed 's/.n.'$defdump'.(....).(..)(..)/\1 \2 \3/'} \
  136. 'is not a backup day for' $xdump >[1=2]
  137. exit 'bad date'
  138. }
  139. $copy $xlast^$dumppath $xpath
  140. }
  141. exit ''