[Bash] MD5 Checks with Script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Bash] MD5 Checks with Script.
# 1  
Old 08-10-2010
[Bash] MD5 Checks with Script.

Hi.
I'm triyng to make a Bash Script that checks (recursively) the MD5 from all the files in a certain directory and compare them against some other check that should be already done and saved in a file.
I've reached to the point where i have the MD5 from the file and the MD5 that the script saved in a file the last time it runs.
But, for some reason, when i compare them, the script always tell me that the MD5 change!
Here is the part of the script that make the comparision:
Code:
     for Archivo in $Archivos; do

     # Run MD5SUM to the file and separe the MD5 and the PATH to make comparisions against the last archive i've generate with this script.
            Arch=`/usr/bin/md5sum $Archivo`
            FileMD5=`echo ${Arch%% *}`
            FilePath=`echo ${Arch##* }`

    # żDoes FilePath exist in the last file generated by this Script?
            Existe=`grep $FilePath $LogMasNuevo`
        # The same: I separe the MD5 and PATH but for the line i took from the last file generated by this script.
            StatusFile=`echo ${Existe##* }`
            StatusMD5=`echo ${Existe%% *}`
            if [ $StatusFile == $FilePath ]; then
        # So the FilePath is the same... im gonna check the MD5 now.

                # To be sure, i remove from the MD5's all the spaces and line breaks...
                FileMD5=`echo $FileMD5 | sed 's/ //g'`
                StatusMD5=`echo $StatusMD5 | sed 's/ //g'`
                FileMD5=`echo -e $FileMD5 | tr -d '\n'`
                StatusMD5=`echo -e $StatusMD5 | tr -d '\n'`

        # żAre the MD5's different?
                if [ $StatusMD5 = $FileMD5 ]; then
                    echo "The MD5 change!"
                    echo $Arch >> $NewLogFile
                else
                    echo "Nothing to do..."
                fi

            else
                # The FilePATH is different, so its a new file!
                echo "The File $Arch " is new!"
                echo $Arch >> $NewLogFile
            fi
        done

For those who want it, the complete script: (Long and with Comments in Spanish!)
Code:
#!/bin/bash

#################################
## Inicio Seccion Configurable ##
#################################
# Historial de LOGs que deseamos almacenar.
HistoriaLogs=2
#################################
### Fin Seccion Configurable ####
#################################


# Funcion Generador de LOGs.
GenerarLogs () {
    PrimeraVes=$1
    echo "Creando Archivo de Log..."
    NewLogFile=$OldLogFile'-'$Fecha'.log'
    # Chequeo la correctitud del Nuevo Archivo de Log generado...
    #if [ "${NewLogFile:0:1}" = "." ]; then
    #   NewLogFile=${NewLogFile:1}
    #fi
    # Genero Listado de Archivos.
    Archivos=`find $Path -type f`
    # Verifico el Parametro, para analizar si es la primera ves que corro el script.
    if [ $PrimeraVes -eq 1 ]; then
        for Archivo in $Archivos; do
            MD5SUM=`/usr/bin/md5sum $Archivo`
            echo $MD5SUM  >> $NewLogFile
        done
    else
        # Busco el Ultimo LOG generado:
        declare -i MasAlto
        declare -i Actual
        Actual=0
        MasAlto=0
        ListadoDeLogs=`ls -1 $Dir`
        for Log in $ListadoDeLogs; do
            Actual=$(echo "$Log" | sed "s/[^0-9]//g")
            if [ $Actual -gt $MasAlto ]; then
                MasAlto=$Actual
            fi
        done
        LogMasNuevo=$OldLogFile'-'$MasAlto'.log'
        # Comienzo al creacion del Nuevo Log: Verifico por cada Archivo en mi arbol su status en el Ultimo LOG creado.

        declare -a Array
        declare -a Array2

        for Archivo in $Archivos; do
            Arch=`/usr/bin/md5sum $Archivo`
            FileMD5=`echo ${Arch%% *}`
            FilePath=`echo ${Arch##* }`
            Dash="'"
            Existe=`grep $FilePath $LogMasNuevo`
            StatusFile=`echo ${Existe##* }`
            StatusMD5=`echo ${Existe%% *}`
            # Verifico si el Archivo existia en el ultimo log.
            if [ $StatusFile == $FilePath ]; then
                # El Archivo Existe... Cambio su MD5?

                # Quitar Espacios...
                FileMD5=`echo $FileMD5 | sed 's/ //g'`
                StatusMD5=`echo $StatusMD5 | sed 's/ //g'`

                # Quitar Saltos de Linea...
                FileMD5=`echo -e $FileMD5 | tr -d '\n'`
                StatusMD5=`echo -e $StatusMD5 | tr -d '\n'`

                if [ $StatusMD5 = $FileMD5 ]; then
                    # Cambio su MD5 -> Una nueva version fue subida en la fecha de ultima modificacion.
                    echo "EL MD5 CAMBIO!"
                    echo $Arch >> $NewLogFile
                else
                    # Su MD5 se mantuvo identico -> No hubo cambios.
                    echo "Todo quedo Igual..."
                    echo $Arch >> $NewLogFile
                fi
            else
                # El Archivo no existe -> Se Subio un archivo nuevo!
                echo "EL ARCHIVO " $Arch " ES NUEVO!"
                echo $Arch >> $NewLogFile
            fi
        done
        # Finalizo verificando en base al ultimo LOG creado si al nuevo LOG recien creado le falta algo... Pudo haberse Eliminado un archivo!
        #cat $LogMasNuevo | while read line; do
        #    StatusFile=`echo ${line##* }`
        #    # Parseo la Linea y verifico solamente que el Path exista en el nuevo. Lo demas ya fue verificado antes.
        #    Existe=`grep $Dash$StatusFile$Dash $NewLogFile | wc -l`
        #    echo $Existe
        #    if [ $Existe -eq 0 ]; then
        #       # El archivo no existe -> Fue Eliminado!
        #       echo "EL ARCHIVO " $Linea " FUE ELIMINADO!"
        #    fi
        #done
    fi
}


# Parametros de Script.
Dir=`dirname $0`
File=`basename $0`

# Solo permito que se ingrese un unico parametro.
if [[ $# -lt 1 || $# -gt 1 ]]
then
    echo "Se requiere parametro: Directorio."
    exit
fi

Path=$1
# Verifico que exista el Directorio ingresado por Parametro.
if ! [ -d $Path ]; then
    echo "Error: $Path no es un Directorio existente en el sistema."
    exit
else
    # Chequeo que el Parametro (Directorio) ingresado contenga un "/" al final, de no contenerlo lo agrego.
    LastChar=${Path#${Path%?}}
    if ! [ $LastChar == "/" ]; then
        Path=$Path'/'
    fi
fi

# Seteo Formato de Fecha a AnioMesDiaHoraMinuto
Fecha=`date +%y%m%d%H%M`
# Chequeo si existe algun  archivo de LOG perteneciente a este Script (NombreDelScript-Fecha.log).
OldLogFile=$Dir'/'$File
# Chequeo la correctitud del Archivo de Log
if [ "${OldLogFile:0:2}" = "./" ]; then
    OldLogFile=${OldLogFile:2}
fi
# Cuento la cantidad de Archivos de LOG antiguos en la carpeta del Script.
declare -i CantidadLogs
CantidadLogs=`ls -1 $OldLogFile*'.log'| wc -l`

if [ $CantidadLogs -eq 0 ]; then
    echo 'Nota: El error "ls: VerificarVersiones.sh*.log: No such file or directory" es normal cuando no existen archivos de LOGs previos (Ej: El Script se corre por primera ves)'
    PrimeraVes=1
    # Genero el archivo de LOG avisando que no compare con otros LOGs antiguos.
    GenerarLogs $PrimeraVes
else
    PrimeraVes=0
    # Busco Archivo de LOG mas reciente...
    # Verifico que HistorialLogs sea un numero entero.
    if [ ! $(echo "$HistoriaLogs" | grep -E "^[0-9]+$") ]
    then
        echo "El parametro HistorialLogs debe ser un numero Entero. Verifique la seccion 'Configurable' del Script."
        exit
    fi
    # Verifico la cantidad de archivos de LOG sea menor que lo Configurado ("HistorialLogs").
    if [ $CantidadLogs -ge $HistoriaLogs ]; then
        Count=0
        # Debo Eliminar LOGs (los mas antiguos) hasta que tenga menos que la cantidad seteada mediante el parametro HistoriaLogs...
        declare -i MasBajo
        declare -i Actual
        declare -i LogSobrantes
        Actual=0
        LogSobrantes=$CantidadLogs-$HistoriaLogs
        while [ $Count -lt $LogSobrantes ]; do
            # Obtengo el LOG mas antiguo y lo Eliminio.
            ListadoDeLogs=`ls -1 $OldLogFile*'.log'`
            MasBajo=$Fecha
            for Log in $ListadoDeLogs; do
                Actual=$(echo "$Log" | sed "s/[^0-9]//g")
                if [[ $Actual -lt $MasBajo ]]; then
                    MasBajo=$Actual
                fi
            done
            rm -f $OldLogFile'-'$MasBajo'.log'
            Count=$[$Count+1]
        done
    fi
    # Genero el archivo de LOG avisando que debe comparar con el ultimo LOG que encuentre.
    GenerarLogs $PrimeraVes
fi

Sorry for my poor English,
Thanks in advance.
Bye!
# 2  
Old 08-17-2010
Hi!
Well it doesnt matter any more, i've finished the script.

Thanks anyway!
Bye.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Multiple checks inside if condition in a bash shell script

Hi, I need to perform the untar and rm operation if the file found is a .tar and does not have test.tar or hello.tar as the file names. Below is the loop to check the same. for tf in *.tar do if ] then found=1 ... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

[Help] Bash script that runs in the background and checks for mails...

Hello! I have got a homework. The bash script runs in the background and checks the user's mailbox and when the user gets a new mail a popup window appears with some text and information about the sender (from who and when).I have no idea how to start, any help would be appreciated! Thank you:) (1 Reply)
Discussion started by: capo2ndfret
1 Replies

3. Shell Programming and Scripting

scp script with MD5

I have more than one file in my directory & I want to SCP them all with MD5 verification of local & remote file, Problem is that the script gets stop after scp one file. Below is the code. #!/usr/bin/bash cd /application/datafolder/my/ ls > my_file_list.txt while read fname do ... (7 Replies)
Discussion started by: m_raheelahmed
7 Replies

4. Shell Programming and Scripting

Script function which checks if itself is already running

Hi All, I have a cron job set up which is set to run every 10 seconds. What I need to do is have the script do a check to see if it is already running such that if it is running it wont fire up additional instances and processes according to its normal process. For example if I have a script... (4 Replies)
Discussion started by: landossa
4 Replies

5. Shell Programming and Scripting

Script to do the following checks

Hi , I need a script for processing below scenario. I have to check daily by doing ftp IP to check it is logging or not. So i want this activity to be automated such that if login succesful i will get "FTP LOGIN SUCCESS" in a log file and if fails i want the error message in the same log... (1 Reply)
Discussion started by: sv0081493
1 Replies

6. Shell Programming and Scripting

Script to performs checks

Hi , I need a script which performs below activity I have one file named "testfile" in 9 different directories with same name. I want to perform below action with each testfile of each directory. if ; then mv listfiles listfiles_`date +%b%y` else echo No Such files fi ... (4 Replies)
Discussion started by: sv0081493
4 Replies

7. Shell Programming and Scripting

Help with MD5 script

Hi, I tried to write script, which would be able to generate MD5 sums into txt file.. But It won't work.. (I've been trying to fix that over 4 hours, but nothing helps) Here it is #!/bin/bash FILE="nothing1" POST="nothing2" I=1 while do FILE=`ls -ltR | grep "^-" | tr -s "... (1 Reply)
Discussion started by: TheBarnacle
1 Replies

8. Shell Programming and Scripting

Create md5 sums and archive the resulting md5 files

Hello everyone, I am looking to basically creating md5sum files for all iso files in a directory and archive the resulting md5 files into a single archive in that very same directory. I worked out a clumsy solution such as: #find files for which md5sum are to be created and store the... (1 Reply)
Discussion started by: SurfTranquille
1 Replies

9. UNIX Desktop Questions & Answers

Script to compare md5

From couple of hours i try to do a script to compare file MD5. Help would be very appreciated :) First i read the md5 stored into a text file (no problem): md5=`grep -P "^{32}" $file` then i want to calculate the checksum and store it : cmd5=`md5sum licence.gvcl` then problems arrives, i... (5 Replies)
Discussion started by: giova
5 Replies

10. Shell Programming and Scripting

Want to make a script that checks connection protocol

Hello all, I currently connect to several servers multiple times a day. Most of the time I connect via SSH through the terminal emulator poderosa (my personal favorite), but sometimes I connect via telnet through xstart because I need it to export a GUI. What I want to do is add something to... (3 Replies)
Discussion started by: DeCoTwc
3 Replies
Login or Register to Ask a Question