home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 9
/
Freelog009.iso
/
BAS
/
Systeme
/
ShellUnix
/
NOACCESS.SH
< prev
next >
Wrap
Linux/UNIX/POSIX Shell Script
|
1998-07-09
|
14KB
|
393 lines
#!/bin/sh
#----------------------------------------------------------------------
# NOM
# NoAccess = Liste des fichiers non accedes depuis une date donnee.
#
# SYNTAXE
# NoAccess -h|H (aide)
# NoAccess [-b] [-T +nb] [-t nb] [-l login]
#
#----------------------------------------------------------------------
# VERSION
# 2.0
#
# DESCRIPTION
#
# * Elle recherche les fichiers (se trouvant sous son
# Home directory) non accedes en lecture et ecriture depuis plus
# d'un certain nombre de jours (-t +nb).
# Plus precisement , elle comptabilise le nombre total de ces
# fichiers et recupere le nom du ou des plus grands fichiers .
#
# * Il est egalement possible de ne recuperer que les fichiers
# qui depassent une certaine taille (-T nb).
#
# * Le mode Brut permet d'effectuer des "formatages" personnels
# en effectuant un traitement particulier sur les resultats. Par
# defaut la procedure presente les informations sous la forme d'un
# tableau :
#
# ** Exemple de resultat en mode Brut :
# NbNoAccess: 1361 +3
# Sup_1_NoAccess: 3-Feb-15:13 1204224 ./bin/hp/display +3
# Sup_1_NoAccess: 18-Feb-08:54 1486776 ./Tips/TipsHP +3
# Sup_1_NoAccess: 8-Mar-21:22 3225088 ./windows/word/divers/wrkdoc.doc +3
# Sup_1_NoAccess: 5-Jun-07:48 2171392 ./windows/word/divers/doc.doc +3
# Sup_1_NoAccess: 19-Feb-18:03 1633280 ./windows/word/tmp/acc_new.ppt +3
# Sup_1_NoAccess: 17-Apr-14:34 2764075 ./windows/mail/aconserver.txt +3
#
# ** Exemple de resultat formate par la procedure :
# Recherche effectuee a partir du repertoire : /home/toto
# +-------------------------------------------------------------------------+
# | Vous avez 1361 fichiers non accedes depuis +3 jours. |
# | Liste des fichiers de plus de 1 Mo - La taille des fichiers est en Ko |
# +----------+-------------+------------------------------------------------+
# | TAILLE | DATE ACCES | REPERTOIRE ET NOM DE FICHIER |
# +----------+-------------+------------------------------------------------+
# | 1204224 | 3-Feb-15:13 | ./bin/hp/display
# | 1486776 |18-Feb-08:54 | ./Tips/TipsHP
# | 3225088 | 8-Mar-21:22 | ./windows/word/divers/wrkdoc.doc
# | 2171392 | 5-Jun-07:48 | ./windows/word/divers/doc.doc
# | 1633280 |19-Feb-18:03 | ./windows/word/tmp/acc_new.ppt
# | 2764075 |17-Apr-14:34 | ./windows/mail/aconserver.txt
# +----------+-------------+------------------------------------------------+
#
# OPTION(S)
# -h|H aide courte ou longue.
#
# -T +nb fichiers non accedes (ecriture / lecture)
# depuis plus de nb (nombre de jours). Si l'option n'est
# pas precise, la recherche s'effectue sur les fichiers
# de plus de 15 jours (+15).
#
# -t nb nb taille des fichiers en Mega-octets.
#
# -l login Pour un login particulier, par defaut le login
# courant est utilise.
#
# -b Mode Brut, les informations ne sont pas formatees
# et sont envoyees sur la sortie standard sous forme
# de colonne avec un libelle (1ere colonne) designant
# le type d'information.
#
# EXEMPLE(S)
#
# * Recherche des fichiers non accedes depuis plus de 30 jours
# dont la taille est superieure a 5 Mo :
# NoAccess -t +30 -T 5
#
# AUTEUR
# Bertrand Le Quellec
#
# CONTACT
# eMail : blq@mygale.org ou blq@wanadoo.fr
# Url : http://www.mygale.org/09/blq/ ou http://perso.wanadoo.fr/blq/
#
# Cree : 10/11/93 - Derniere modification : 30/06/98
#----------------------------------------------------------------------
# Permission to use, copy, modify, distribute, and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Bertrand LE QUELLEC
# not be used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission. Bertrand
# LE QUELLEC makes no representations about the suitability of
# this software for any purpose. It is provided "as is" without
# express or implied warranty.
#
# THIS SOFTWARE IS PROVIDED BY Bertrand LE QUELLEC `AS IS' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Bertrand LE QUELLEC BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#----------------------------------------------------------------------
# ------------------------------------------
# ZONE DE DEFINITION DES VARIABLES GLOBALES:
# ------------------------------------------
NOM_PROCEDURE=`basename $0`
LA_PROCEDURE=$0
CD=`pwd`
AWK=`[ -f /usr/bin/nawk ] && echo "nawk" || echo "awk" `
MODE_COULEUR=0
##
## Variables particulieres:
##
DATE=`date "+%d/%m/%y"`
DEFAUT_USER=`whoami`
HOSTNAME=`hostname`
NISHOST=`ypwhich -m passwd 2> /dev/null`
UNAME=`uname`
RSH=`[ "$UNAME" = "HP-UX" -o "$UNAME" = "AIX" ] && echo "remsh" || echo "rsh"`
#----------------------------------------------------------------------
# -----------------------------------------
# ZONE DE DEFINITION DES FONCTIONS LOCALES:
# -----------------------------------------
##
## Affiche un message en rouge si l'on se trouve dans une fenetre hpterm
## et se met en attente de 2 secondes.
## Il est possible de recuperer le code retour de la fonction.
##
Erreur()
{
[ $# -eq 0 ] && return -1
echo "\n\t$*"
sleep 2
return 0
}
##
## En cas d'une interruption la fonction suivante est executee.
## Les interruptions sont des signaux envoyes au processus du script,
## par la commande Unix: kill(1). La "capture" de ces signaux est
## identifiee par la commande Shell: trap definie apres la zone des
## options.
##
TrapInterrup()
{
# Vous pouvez executer des instructions avant une sortie
# par signal (ou brutale!).
for fich in $tpfnoacc3 $tpfnoacc4 $tpfnoacc2
do
[ -f $fich ] && rm -f $fich
done
exit 1
}
##
## Affiche l'aide indiquee en en-tete du script shell.
## Ne pas supprimer car cette fonction est utilisee a l'appel de la
## procedure avec les options -H ou -h
##
AideProc()
{
fichier=$1
niveau=$2
[ -z "$fichier" ] && {
Erreur "AideProc(): nombre d'argument incorect."
return 0
}
[ ! -f "$fichier" ] && {
Erreur "AideProc(): impossible d'atteindre le fichier $fichier."
return 0
}
[ ${niveau:=2} = 2 ] && {
${AWK} 'BEGIN {i=2}
/^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
if(--i<=0)exit
next
}
/^#[ ]*[A-Z]*[ ]*$/{
next
}
/^#[^!]/{
print substr($0,1,80)
}
/^#[ \t]*$/{
print ""
}
' $fichier | sed -e 's/^#[ ]*//'
} || {
${AWK} '\
/^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
if(--i<=0) exit
next
}
/^#[^!]/{
print substr($0,2,80)
}
/^#[ \t]*$/{
print ""
}
' i=$niveau $fichier | pg -e
}
}
Temp="+15"
TailleMo="5"
LOGIN="$DEFAUT_USER"
MODE_BRUT=0
min=0
nb_arg=$#
n=`expr $nb_arg / 2`
#----------------------------------------------------------------------
# -------------------------
# RECUPERATION DES OPTIONS:
# -------------------------
set -- `getopt hHxcbt:T:l: $*`
for opt in $*
do
case $opt in
-h) AideProc $LA_PROCEDURE ; exit 0 ;;
-H) AideProc $LA_PROCEDURE 3 ; exit 0 ;;
-x) set -x ; shift ;;
-c) shift ; MODE_COULEUR=1 ;;
-b) shift ; MODE_BRUT=1 ;;
-T) shift ; Temp=$1 ; shift ;;
-t) shift ; TailleMo=$1 ; shift ; min=1 ;;
-l) shift ; LOGIN=$1 ; shift ;;
--) shift ; break ;;
esac
done
#----------------------------------------------------------------------
# AUTRES DEFINITIONS:
# -------------------
trap TrapInterrup 3 2 15 # Recuperation des interruptions 3, 2 et 15
##
## Caracteres speciaux valable uniquement sur HP:
## Permet d'obtenir de la couleur sur des fenetres hp-term.
##
[ $MODE_COULEUR -eq 1 ] && {
[ "$TERM" = "hpterm" ] && {
BLANC="\033&v0S"; ROUGE="\033&v1S"; VERT="\033&v2S"; JAUNE="\033&v3S"
BLEU="\033&v4S"; MAGENTA="\033&v5S"; CYAN="\033&v6S"; NOIR="\033&v7S"
SOULIGNE="\033&dD"; INVERSE="\033&dB"; INVSOUS="\033&dF"; NORM="\033&d@"
NORMAL=${NORM}${BLANC}
} || {
NOIR=`tput setaf 0 2> /dev/null`; ROUGE=`tput setaf 1 2> /dev/null`
VERT=`tput setaf 2 2> /dev/null`; JAUNE=`tput setaf 3 2> /dev/null`
BLEU=`tput setaf 4 2> /dev/null`; MAGENTA=`tput setaf 5 2> /dev/null`
CYAN=`tput setaf 6 2> /dev/null`; BLANC=`tput setaf 7 2> /dev/null`
SOULIGNE=`tput smul 2> /dev/null`; INVERSE=`tput smso 2> /dev/null`
INVSOUS=${SOULIGNE}${INVERSE}; NORM=`tput sgr0 2> /dev/null`
NORMAL=${NORM}${BLANC}
}
} || {
NOIR=""; ROUGE=""
VERT=""; JAUNE=""
BLEU=""; MAGENTA=""
CYAN=""; BLANC=""
SOULIGNE=""; INVERSE=""
INVSOUS=${SOULIGNE}${INVERSE}; NORM=""
NORMAL=${NORM}${BLANC}
}
#----------------------------------------------------------------------
# DEBUT ZONE DU PROGRAMME:
#----------------------------------------------------------------------
flag=0
taille_sup=0
true=
tpfnoacc2=/tmp/fnoacc2.$$
tpfnoacc3=/tmp/fnoacc3.$$
tpfnoacc4=/tmp/fnoacc4.$$
#----------------------------------------------------------------------
# 1 Mo --> 2048 blocks
# taillebytes = tailleblocs * 512
# 1 Mo --> 2048*512 = 1048576 bytes
#----------------------------------------------------------------------
[ ! -z "$TailleMo" ] && Tailleby=`expr $TailleMo \* 1048576`
[ -n "$NISHOST" ] && HomeRep=`ypcat passwd | ${AWK} -F: '{ if($1==login) print $6 }' login=$LOGIN` || {
HomeRep=`grep $LOGIN /etc/passwd | cut -d: -f6`
}
diskuser=""
[ ! -z "$HomeRep" ] && diskuser=`echo $HomeRep | cut -d"/" -f2`
########################################################
# Recherche de fichiers avec une condition sur la taille
########################################################
cd $HomeRep
find . -type f -atime $Temp -exec ls -lg {} \; 2>/dev/null | grep -v "\/$diskuser\/$user\/\." > $tpfnoacc2
[ -s "$tpfnoacc2" ] && {
###################################################
# Nombre de fichiers non accedes depuis $Temp (+nb)
###################################################
nb_fich=`wc -l $tpfnoacc2 | cut -d" " -f1`
#####################################################################################
# Recherche du nom des fichiers non accedes dont la taille est superieure a $TailleMo
#####################################################################################
$AWK '{
if ( $4 > Tby ) {
nom_fichier = $NF
printf("Sup_%s_NoAccess:\t%s-%s-%s\t%10s\t%s\t%s\n",
TMo, $6, $5, $7, $4, nom_fichier, t)
}
}' Tby="$Tailleby" TMo="$TailleMo" t="$Temp" $tpfnoacc2 > $tpfnoacc4
[ -s "$tpfnoacc4" ] && {
echo "NbNoAccess:\t$nb_fich\t$Temp" > $tpfnoacc3
cat $tpfnoacc4 >> $tpfnoacc3
} || {
echo "NbNoAccess:\t$nb_fich\t$Temp" > $tpfnoacc3
echo "Sup_${TailleMo}_NoAccess:\t-\t0\t0\t$Temp" >> $tpfnoacc3
}
} || {
echo "NbNoAccess:\t0\t$Temp" > $tpfnoacc3
}
[ -f $tpfnoacc3 ] && {
[ $MODE_BRUT -eq 1 ] && cat $tpfnoacc3 || {
${AWK} ' BEGIN { flag=0 } /NbNoAccess/ {
print ""
print " Recherche effectuee a partir du repertoire :", rep
print "+-----------------------------------------------------------------------------------+"
printf("| Vous avez %s%5s%s fichiers non accedes depuis %s%3s%s jours. |\n",
coul, $2, norm, coul, jour, norm)
}
/Sup_/ {
if ($3 != 0)
{
if (flag == 0) {
flag=1
printf("| Liste des fichiers de plus de %s%3s%s Mo - La taille des fichiers est en Ko |\n",
coul, mega, norm)
print "+----------+-------------+----------------------------------------------------------+"
print "| TAILLE | DATE ACCES | REPERTOIRE ET NOM DE FICHIER |"
print "+----------+-------------+----------------------------------------------------------+"
}
printf("|%s%9s%s |%12s | %s \n", coul, $3, norm, $2, $4)
}
} END {
if (flag > 0)
print "+----------+-------------+----------------------------------------------------------+\n"
else
print "+-----------------------------------------------------------------------------------+\n"
}' rep=$HomeRep mega=$TailleMo jour=$Temp coul=${VERT} norm=${NORMAL} $tpfnoacc3
}
}
#----------------------------------------------------------------------
TrapInterrup