Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- #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
- #
- #Author:jMa
- #
- declare -a carpetas=()
- declare -a carpetas2=()
- i=0
- j=0
- function crearrays {
- cd $1; find -maxdepth 1 ! \( -type d \)| cut -c 3- |sort > dir1
- while read elemento
- do
- carpetas=("${carpetas[@]}" "$elemento")
- done < ./dir1
- cd $2; find -maxdepth 1 ! \( -type d \)| cut -c 3- |grep -v "dir2" |sort > dir2
- while read elemento
- do
- carpetas2=("${carpetas2[@]}" "$elemento")
- done < ./dir2
- tamanyo1=${#carpetas[@]}
- tamanyo2=${#carpetas2[@]}
- rm -f $1/dir1
- rm -f $2/dir2
- }
- function diferencia {
- if [ $tamanyo1 -gt 0 ] && [ $tamanyo2 -gt 0 ]
- then
- while true
- do
- fich="${carpetas[$i]}"
- fich2="${carpetas2[$j]}"
- if [ "$fich" == "$fich2" ]
- then
- carpetas[$i]="X"
- carpetas2[$j]="X"
- let i=i+1
- let j=j+1
- elif [ "$fich" < "$fich2" ]
- then
- let i=i+1
- elif [ "$fich" > "$fich2" ]
- then
- let j=j+1
- fi
- if [ $i -eq $tamanyo1 ] || [ $j -eq $tamanyo2 ]
- then
- break
- fi
- done
- fi
- for i in `seq 0 1 $[ ${#carpetas[@]} - 1 ]`
- do
- if [ "${carpetas[$i]}" != "X" ]
- then
- echo -e "*El fichero ["${carpetas[$i]}"] aparece sólo en el directorio $1\n"
- fi
- done
- for j in `seq 0 1 $[ ${#carpetas2[@]} - 1 ]`
- do
- if [ "${carpetas2[$j]}" != "X" ]
- then
- echo -e "*El fichero ["${carpetas2[$j]}"] aparece sólo en el directorio $1\n"
- fi
- done
- }
- function comun {
- if [ $tamanyo1 -gt 0 ] && [ $tamanyo2 -gt 0 ]
- then
- while true
- do
- fich="${carpetas[$i]}"
- fich2="${carpetas2[$j]}"
- if [ "$fich" == "$fich2" ]
- then
- echo "El fichero ["${carpetas[$i]}"] es común a ambos directorios"
- let i=i+1
- let j=j+1
- elif [ "$fich" < "$fich2" ]
- then
- let i=i+1
- elif [ "$fich" > "$fich2" ]
- then
- let j=j+1
- fi
- if [ $i -eq $tamanyo1 ] || [ $j -eq $tamanyo2 ]
- then
- break
- fi
- done
- fi
- }
- while getopts i MYOPTION
- do
- case $MYOPTION in
- i) if test -d "$2"
- then
- if [ -z $3 ]
- then
- echo "Se ha suprimido el segundo parámetro y por tanto el directorio a tomar es $PWD/"
- crearrays $2 $PWD/
- comun
- exit 0
- elif test -d "$3"
- then
- crearrays $2 $3
- comun
- exit 0
- else
- echo "Directorio 2 inexistente"
- exit 1
- fi
- else
- echo "Directorio/s inexistente/s"
- exit 1
- fi
- ;;
- esac
- done
- if test -d "$1"
- then
- if [ -z $2 ]
- then
- crearrays $1 $PWD/
- diferencia
- elif test -d "$2"
- then
- crearrays $1 $2
- diferencia
- else
- echo "Segundo parámetro incorrecto"
- fi
- else
- echo "Parámetro/s incorrecto/s"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement