home *** CD-ROM | disk | FTP | other *** search
/ Freelog 9 / Freelog009.iso / BAS / Systeme / ShellUnix / NOACCESS.SH < prev    next >
Linux/UNIX/POSIX Shell Script  |  1998-07-09  |  14KB  |  393 lines

  1. #!/bin/sh 
  2. #---------------------------------------------------------------------- 
  3. # NOM 
  4. #    NoAccess = Liste des fichiers non accedes depuis une date donnee.
  5. # SYNTAXE 
  6. #    NoAccess -h|H     (aide) 
  7. #    NoAccess [-b] [-T +nb] [-t nb] [-l login]
  8. #---------------------------------------------------------------------- 
  9. # VERSION 
  10. #   2.0
  11. #
  12. # DESCRIPTION 
  13. #
  14. #    * Elle recherche les fichiers (se trouvant sous son
  15. #   Home directory) non accedes en lecture et ecriture depuis plus
  16. #   d'un certain nombre de jours (-t +nb).
  17. #    Plus precisement , elle comptabilise le nombre total de ces
  18. #   fichiers et recupere le nom du ou des plus grands fichiers .
  19. #
  20. #    * Il est egalement possible de ne recuperer que les fichiers
  21. #   qui depassent une certaine taille (-T nb). 
  22. #                               
  23. #    * Le mode Brut permet d'effectuer des "formatages" personnels
  24. #   en effectuant un traitement particulier sur les resultats. Par
  25. #   defaut la procedure presente les informations sous la forme d'un
  26. #   tableau :
  27. #
  28. #   ** Exemple de resultat en mode Brut :
  29. # NbNoAccess:     1361    +3
  30. # Sup_1_NoAccess: 3-Feb-15:13  1204224 ./bin/hp/display +3
  31. # Sup_1_NoAccess: 18-Feb-08:54 1486776 ./Tips/TipsHP +3
  32. # Sup_1_NoAccess: 8-Mar-21:22  3225088 ./windows/word/divers/wrkdoc.doc +3
  33. # Sup_1_NoAccess: 5-Jun-07:48  2171392 ./windows/word/divers/doc.doc +3
  34. # Sup_1_NoAccess: 19-Feb-18:03 1633280 ./windows/word/tmp/acc_new.ppt +3
  35. # Sup_1_NoAccess: 17-Apr-14:34 2764075 ./windows/mail/aconserver.txt +3
  36. #   ** Exemple de resultat formate par la procedure :
  37. # Recherche effectuee a partir du repertoire : /home/toto
  38. # +-------------------------------------------------------------------------+
  39. # | Vous avez  1361 fichiers non accedes depuis  +3 jours.                  |
  40. # | Liste des fichiers de plus de   1 Mo - La taille des fichiers est en Ko |
  41. # +----------+-------------+------------------------------------------------+
  42. # |  TAILLE  |  DATE ACCES |  REPERTOIRE ET NOM DE FICHIER                  |
  43. # +----------+-------------+------------------------------------------------+
  44. # |  1204224 | 3-Feb-15:13 | ./bin/hp/display 
  45. # |  1486776 |18-Feb-08:54 | ./Tips/TipsHP 
  46. # |  3225088 | 8-Mar-21:22 | ./windows/word/divers/wrkdoc.doc 
  47. # |  2171392 | 5-Jun-07:48 | ./windows/word/divers/doc.doc 
  48. # |  1633280 |19-Feb-18:03 | ./windows/word/tmp/acc_new.ppt 
  49. # |  2764075 |17-Apr-14:34 | ./windows/mail/aconserver.txt 
  50. # +----------+-------------+------------------------------------------------+
  51. # OPTION(S) 
  52. #    -h|H        aide courte ou longue. 
  53. #
  54. #    -T +nb      fichiers non accedes (ecriture / lecture)
  55. #                depuis plus de nb (nombre de jours). Si l'option n'est
  56. #                pas precise, la recherche s'effectue sur les fichiers
  57. #                de plus de 15 jours (+15).
  58. #
  59. #    -t nb       nb taille des fichiers en Mega-octets.
  60. #    -l login    Pour un login particulier, par defaut le login
  61. #                courant est utilise.
  62. #    -b          Mode Brut, les informations ne sont pas formatees
  63. #                et sont envoyees sur la sortie standard sous forme
  64. #                de colonne avec un libelle (1ere colonne) designant
  65. #                le type d'information.
  66. # EXEMPLE(S) 
  67. #
  68. #    * Recherche des fichiers non accedes depuis plus de 30 jours
  69. #    dont la taille est superieure a 5 Mo :
  70. #              NoAccess -t +30 -T 5
  71. # AUTEUR
  72. #   Bertrand Le Quellec 
  73. # CONTACT
  74. #   eMail : blq@mygale.org ou blq@wanadoo.fr
  75. #   Url   : http://www.mygale.org/09/blq/ ou http://perso.wanadoo.fr/blq/
  76. #
  77. # Cree : 10/11/93 - Derniere modification : 30/06/98
  78. #---------------------------------------------------------------------- 
  79. # Permission to use, copy, modify, distribute, and sell this software
  80. # and its documentation for any purpose is hereby granted without fee,
  81. # provided that the above copyright notice appear in all copies and
  82. # that both that copyright notice and this permission notice appear in
  83. # supporting documentation, and that the name of Bertrand LE QUELLEC    
  84. # not be used in advertising or publicity pertaining to distribution 
  85. # of the software without specific, written prior permission. Bertrand 
  86. # LE QUELLEC makes no representations about the suitability of
  87. # this software for any purpose. It is provided "as is" without
  88. # express or implied warranty.
  89. #
  90. # THIS SOFTWARE IS PROVIDED BY Bertrand LE QUELLEC `AS IS' AND ANY
  91. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  92. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  93. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Bertrand LE QUELLEC BE
  94. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  95. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  96. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  97. # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  98. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  99. # OR OTHERWISE)     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  100. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  101. #---------------------------------------------------------------------- 
  102.  
  103. # ------------------------------------------
  104. # ZONE DE DEFINITION DES VARIABLES GLOBALES:
  105. # ------------------------------------------
  106. NOM_PROCEDURE=`basename $0`
  107. LA_PROCEDURE=$0
  108.  
  109. CD=`pwd`
  110. AWK=`[ -f /usr/bin/nawk ] && echo "nawk" || echo "awk" `
  111.  
  112. MODE_COULEUR=0
  113.  
  114. ##
  115. ## Variables particulieres:
  116. ##
  117. DATE=`date "+%d/%m/%y"`
  118. DEFAUT_USER=`whoami`
  119. HOSTNAME=`hostname`
  120. NISHOST=`ypwhich -m passwd 2> /dev/null`
  121. UNAME=`uname`
  122. RSH=`[ "$UNAME" = "HP-UX" -o "$UNAME" = "AIX" ] && echo "remsh" || echo "rsh"`
  123.  
  124.  
  125. #---------------------------------------------------------------------- 
  126. # ----------------------------------------- 
  127. # ZONE DE DEFINITION DES FONCTIONS LOCALES:
  128. # -----------------------------------------
  129.  
  130. ##
  131. ## Affiche un message en rouge si l'on se trouve dans une fenetre hpterm
  132. ## et se met en attente de 2 secondes.
  133. ## Il est possible de recuperer le code retour de la fonction.
  134. ##
  135. Erreur()
  136. {
  137.     [ $# -eq 0 ] && return -1
  138.  
  139.     echo "\n\t$*"
  140.     sleep 2
  141.  
  142.     return 0
  143. }
  144.  
  145.  
  146. ##
  147. ## En cas d'une interruption la fonction suivante est executee.
  148. ## Les interruptions sont des signaux envoyes au processus du script,
  149. ## par la commande Unix: kill(1). La "capture" de ces signaux est
  150. ## identifiee par la commande Shell: trap definie apres la zone des
  151. ## options.
  152. ##
  153. TrapInterrup()
  154. {
  155.     # Vous pouvez executer des instructions avant une sortie 
  156.     # par signal (ou brutale!).
  157.  
  158.     for fich in $tpfnoacc3 $tpfnoacc4 $tpfnoacc2
  159.     do
  160.         [ -f $fich ] && rm -f $fich
  161.     done
  162.  
  163.     exit 1
  164. }
  165.  
  166.  
  167. ##
  168. ## Affiche l'aide indiquee en en-tete du script shell.
  169. ## Ne pas supprimer car cette fonction est utilisee a l'appel de la
  170. ## procedure avec les options -H ou -h
  171. ##
  172. AideProc()
  173. {
  174.     fichier=$1
  175.     niveau=$2
  176.  
  177.     [ -z "$fichier" ] && {
  178.         Erreur "AideProc(): nombre d'argument incorect."
  179.         return 0
  180.         }
  181.  
  182.     [ ! -f "$fichier" ] && {
  183.         Erreur "AideProc(): impossible d'atteindre le fichier $fichier."
  184.         return 0
  185.     }
  186.  
  187.     [ ${niveau:=2} = 2 ] && {
  188.         ${AWK} 'BEGIN {i=2}
  189.             /^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
  190.                 if(--i<=0)exit
  191.                 next
  192.             }
  193.             /^#[     ]*[A-Z]*[     ]*$/{
  194.                 next
  195.             }
  196.             /^#[^!]/{
  197.                 print substr($0,1,80)
  198.             }
  199.             /^#[ \t]*$/{
  200.                 print ""
  201.             }
  202.         ' $fichier | sed -e 's/^#[     ]*//'
  203.     } || {
  204.         ${AWK} '\
  205.             /^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
  206.                 if(--i<=0) exit
  207.                 next
  208.             }
  209.             /^#[^!]/{
  210.                 print substr($0,2,80)
  211.             }
  212.             /^#[ \t]*$/{
  213.                 print ""
  214.             }
  215.         ' i=$niveau $fichier | pg -e
  216.     }
  217. }
  218.  
  219. Temp="+15"
  220. TailleMo="5"
  221. LOGIN="$DEFAUT_USER"
  222. MODE_BRUT=0
  223. min=0
  224.  
  225. nb_arg=$#
  226. n=`expr $nb_arg / 2`
  227.  
  228. #----------------------------------------------------------------------
  229. # ------------------------- 
  230. # RECUPERATION DES OPTIONS:
  231. # -------------------------
  232. set -- `getopt hHxcbt:T:l: $*`                
  233. for opt in $*                                            
  234. do                                                             
  235.     case $opt in                                           
  236.         -h) AideProc $LA_PROCEDURE ; exit 0 ;;
  237.            -H) AideProc $LA_PROCEDURE 3 ; exit 0 ;;
  238.  
  239.         -x) set -x ; shift ;;
  240.  
  241.         -c) shift ; MODE_COULEUR=1 ;;
  242.  
  243.         -b) shift ; MODE_BRUT=1 ;;
  244.  
  245.         -T) shift ; Temp=$1 ; shift ;;
  246.         -t) shift ; TailleMo=$1 ; shift ; min=1 ;;
  247.         -l) shift ; LOGIN=$1 ; shift ;;
  248.  
  249.         --) shift ; break ;;
  250.     esac
  251. done
  252.  
  253. #----------------------------------------------------------------------
  254. # AUTRES DEFINITIONS:
  255. # -------------------
  256. trap TrapInterrup 3 2 15    # Recuperation des interruptions 3, 2 et 15
  257.  
  258. ##
  259. ## Caracteres speciaux valable uniquement sur HP:
  260. ## Permet d'obtenir de la couleur sur des fenetres hp-term.
  261. ##
  262. [ $MODE_COULEUR -eq 1 ] && {
  263.     [ "$TERM" = "hpterm" ] && {
  264.         BLANC="\033&v0S"; ROUGE="\033&v1S"; VERT="\033&v2S"; JAUNE="\033&v3S"
  265.         BLEU="\033&v4S"; MAGENTA="\033&v5S"; CYAN="\033&v6S"; NOIR="\033&v7S"
  266.            SOULIGNE="\033&dD"; INVERSE="\033&dB"; INVSOUS="\033&dF"; NORM="\033&d@"
  267.         NORMAL=${NORM}${BLANC}
  268.     } || {
  269.         NOIR=`tput setaf 0 2> /dev/null`; ROUGE=`tput setaf 1 2> /dev/null`
  270.         VERT=`tput setaf 2 2> /dev/null`; JAUNE=`tput setaf 3 2> /dev/null`
  271.         BLEU=`tput setaf 4 2> /dev/null`; MAGENTA=`tput setaf 5 2> /dev/null`
  272.         CYAN=`tput setaf 6 2> /dev/null`; BLANC=`tput setaf 7 2> /dev/null`
  273.         SOULIGNE=`tput smul 2> /dev/null`; INVERSE=`tput smso 2> /dev/null`
  274.         INVSOUS=${SOULIGNE}${INVERSE}; NORM=`tput sgr0 2> /dev/null`
  275.         NORMAL=${NORM}${BLANC}
  276.     }
  277. } || {
  278.     NOIR=""; ROUGE=""
  279.     VERT=""; JAUNE=""
  280.     BLEU=""; MAGENTA=""
  281.        CYAN=""; BLANC=""
  282.     SOULIGNE=""; INVERSE=""
  283.     INVSOUS=${SOULIGNE}${INVERSE}; NORM=""
  284.     NORMAL=${NORM}${BLANC}
  285. }
  286.  
  287.  
  288. #----------------------------------------------------------------------
  289. # DEBUT ZONE DU PROGRAMME:
  290. #----------------------------------------------------------------------
  291. flag=0
  292. taille_sup=0
  293. true=
  294. tpfnoacc2=/tmp/fnoacc2.$$
  295. tpfnoacc3=/tmp/fnoacc3.$$
  296. tpfnoacc4=/tmp/fnoacc4.$$
  297.  
  298.  
  299. #----------------------------------------------------------------------
  300. # 1 Mo --> 2048 blocks
  301. # taillebytes = tailleblocs * 512
  302. # 1 Mo --> 2048*512 = 1048576 bytes
  303. #----------------------------------------------------------------------
  304. [ ! -z "$TailleMo" ] && Tailleby=`expr $TailleMo \* 1048576`
  305.  
  306. [ -n "$NISHOST" ] && HomeRep=`ypcat passwd | ${AWK} -F: '{ if($1==login) print $6 }' login=$LOGIN` || {
  307.     HomeRep=`grep $LOGIN /etc/passwd | cut -d: -f6`
  308. }
  309.  
  310. diskuser=""
  311. [ ! -z "$HomeRep" ] && diskuser=`echo $HomeRep | cut -d"/" -f2`
  312.  
  313.  
  314. ########################################################
  315. # Recherche de fichiers avec une condition sur la taille
  316. ########################################################
  317. cd $HomeRep
  318. find . -type f -atime $Temp -exec ls -lg {} \; 2>/dev/null | grep -v "\/$diskuser\/$user\/\." > $tpfnoacc2
  319.  
  320. [ -s "$tpfnoacc2" ] && {
  321.     ###################################################
  322.     # Nombre de fichiers non accedes depuis $Temp (+nb)
  323.     ###################################################
  324.     nb_fich=`wc -l $tpfnoacc2 | cut -d" " -f1`
  325.  
  326.     #####################################################################################
  327.     # Recherche du nom des fichiers non accedes dont la taille est superieure a $TailleMo
  328.     #####################################################################################
  329.     $AWK '{
  330.         if ( $4 > Tby ) {
  331.                nom_fichier = $NF
  332.                printf("Sup_%s_NoAccess:\t%s-%s-%s\t%10s\t%s\t%s\n",
  333.                     TMo, $6, $5, $7, $4, nom_fichier, t)
  334.            }
  335.     }' Tby="$Tailleby" TMo="$TailleMo" t="$Temp" $tpfnoacc2 > $tpfnoacc4
  336.  
  337.     [ -s "$tpfnoacc4" ] && {
  338.         echo "NbNoAccess:\t$nb_fich\t$Temp" > $tpfnoacc3
  339.         cat $tpfnoacc4 >> $tpfnoacc3
  340.     } || {
  341.         echo "NbNoAccess:\t$nb_fich\t$Temp" > $tpfnoacc3
  342.         echo "Sup_${TailleMo}_NoAccess:\t-\t0\t0\t$Temp" >> $tpfnoacc3
  343.     }
  344. } || {
  345.     echo "NbNoAccess:\t0\t$Temp" > $tpfnoacc3
  346. }
  347.  
  348. [ -f $tpfnoacc3 ] && {
  349.     [ $MODE_BRUT -eq 1 ] && cat $tpfnoacc3 || {
  350.         ${AWK} ' BEGIN { flag=0 } /NbNoAccess/ {
  351.             print ""
  352.             print " Recherche effectuee a partir du repertoire :", rep
  353.             print "+-----------------------------------------------------------------------------------+"
  354.             printf("| Vous avez %s%5s%s fichiers non accedes depuis %s%3s%s jours.                            |\n",
  355.                     coul, $2, norm, coul, jour, norm)
  356.         }
  357.         /Sup_/ {
  358.             if ($3 != 0)
  359.             {
  360.                 if (flag == 0) {
  361.                     flag=1
  362.                         
  363.                     printf("| Liste des fichiers de plus de %s%3s%s Mo - La taille des fichiers est en Ko           |\n",
  364.                                 coul, mega, norm)
  365.                     print "+----------+-------------+----------------------------------------------------------+"
  366.                     print "|  TAILLE  |  DATE ACCES |  REPERTOIRE ET NOM DE FICHIER                            |"
  367.                     print "+----------+-------------+----------------------------------------------------------+"
  368.                 }
  369.  
  370.                 printf("|%s%9s%s |%12s | %s \n", coul, $3, norm, $2, $4)
  371.             }
  372.         } END {
  373.             if (flag > 0)
  374.                 print "+----------+-------------+----------------------------------------------------------+\n"
  375.             else
  376.                 print "+-----------------------------------------------------------------------------------+\n"
  377.         }' rep=$HomeRep mega=$TailleMo jour=$Temp coul=${VERT} norm=${NORMAL} $tpfnoacc3
  378.     }
  379. }
  380.  
  381. #----------------------------------------------------------------------
  382. TrapInterrup
  383.  
  384.