Advertisement
hervasiop12345

DIFFD

Jul 21st, 2011
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.48 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. #Description: Muestra la intersección o diferencia entre dos directorios. Se puede suprimir el segundo parámetro y entonces se tomará el directorio actual y mostrara la diferencia. Si se usa la opción -i muestra la intesección y también es posible con esta opción suprimir el segundo parámetro
  4. #
  5. #Author:jMa
  6. #
  7.  
  8. declare -a carpetas=()
  9. declare -a carpetas2=()
  10. i=0
  11. j=0
  12.  
  13. function crearrays {
  14.      cd $1; find -maxdepth 1 ! \( -type d \)| cut -c 3- |sort > dir1
  15.      while read elemento
  16.      do
  17.  
  18.     carpetas=("${carpetas[@]}" "$elemento")
  19.  
  20.      done < ./dir1
  21.      cd $2; find -maxdepth 1 ! \( -type d \)| cut -c 3- |grep -v "dir2" |sort > dir2    
  22.      while read elemento
  23.      do
  24.  
  25.        carpetas2=("${carpetas2[@]}" "$elemento")
  26.  
  27.      done < ./dir2    
  28.      
  29.      tamanyo1=${#carpetas[@]}
  30.      tamanyo2=${#carpetas2[@]}
  31.      rm -f $1/dir1
  32.      rm -f $2/dir2
  33. }
  34.  
  35. function diferencia {
  36.    
  37.     if [ $tamanyo1 -gt 0 ] && [ $tamanyo2 -gt 0 ]
  38.     then
  39.          
  40.       while true
  41.       do
  42.         fich="${carpetas[$i]}"
  43.         fich2="${carpetas2[$j]}"
  44.  
  45.         if [ "$fich" == "$fich2" ]
  46.         then
  47.           carpetas[$i]="X"
  48.           carpetas2[$j]="X"                  
  49.           let i=i+1
  50.           let j=j+1
  51.  
  52.         elif [ "$fich" < "$fich2" ]
  53.         then
  54.  
  55.           let i=i+1
  56.      
  57.         elif [ "$fich" > "$fich2" ]
  58.         then
  59.  
  60.           let j=j+1
  61.      
  62.         fi
  63.  
  64.         if [ $i -eq $tamanyo1 ] || [ $j -eq $tamanyo2 ]
  65.         then
  66.           break
  67.         fi
  68.       done
  69.     fi
  70.  
  71.     for i in `seq 0 1 $[ ${#carpetas[@]} - 1 ]`
  72.     do
  73.  
  74.       if [ "${carpetas[$i]}" != "X" ]
  75.       then
  76.  
  77.         echo -e "*El fichero ["${carpetas[$i]}"] aparece sólo en el directorio $1\n"
  78.  
  79.       fi
  80.     done
  81.    
  82.     for j in `seq 0 1 $[ ${#carpetas2[@]} - 1 ]`
  83.     do
  84.  
  85.       if [ "${carpetas2[$j]}" != "X" ]
  86.       then
  87.  
  88.         echo -e "*El fichero ["${carpetas2[$j]}"] aparece sólo en el  directorio $1\n"
  89.  
  90.       fi
  91.     done
  92. }
  93.  
  94. function comun {
  95.  
  96.     if [ $tamanyo1 -gt 0 ] && [ $tamanyo2 -gt 0 ]
  97.     then
  98.          
  99.       while true
  100.       do
  101.         fich="${carpetas[$i]}"
  102.         fich2="${carpetas2[$j]}"
  103.  
  104.         if [ "$fich" == "$fich2" ]
  105.         then    
  106.           echo "El fichero ["${carpetas[$i]}"] es común a ambos directorios"
  107.           let i=i+1
  108.           let j=j+1
  109.  
  110.         elif [ "$fich" < "$fich2" ]
  111.         then
  112.  
  113.           let i=i+1
  114.      
  115.         elif [ "$fich" > "$fich2" ]
  116.         then
  117.  
  118.           let j=j+1
  119.      
  120.         fi
  121.  
  122.         if [ $i -eq $tamanyo1 ] || [ $j -eq $tamanyo2 ]
  123.         then
  124.           break
  125.         fi
  126.       done
  127.     fi
  128. }
  129.  
  130. while getopts i MYOPTION
  131. do
  132. case $MYOPTION in
  133.   i) if test -d "$2"
  134.      then  
  135.        if [ -z $3 ]
  136.        then
  137.          echo "Se ha suprimido el segundo parámetro y por tanto el directorio a tomar es $PWD/"
  138.          crearrays $2 $PWD/
  139.          comun        
  140.          exit 0  
  141.        elif test -d "$3"
  142.        then                
  143.          crearrays $2 $3
  144.          comun
  145.          exit 0
  146.        else
  147.          echo "Directorio 2 inexistente"
  148.          exit 1
  149.        fi
  150.      else
  151.        echo "Directorio/s inexistente/s"
  152.        exit 1
  153.      fi
  154.      ;;
  155. esac
  156. done
  157.  
  158. if test -d "$1"
  159. then
  160.   if [ -z $2 ]
  161.   then
  162.     crearrays $1 $PWD/
  163.     diferencia    
  164.   elif test -d "$2"
  165.   then    
  166.     crearrays $1 $2
  167.     diferencia
  168.   else
  169.     echo "Segundo parámetro incorrecto"
  170.   fi
  171. else
  172.   echo "Parámetro/s incorrecto/s"
  173. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement