Advertisement
Guest User

Buscador Ficheros

a guest
Jan 26th, 2020
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.69 KB | None | 0 0
  1. # !/bin/bash
  2. # Proyecto Final Laboratorio - Buscador de ficheros
  3. # Sergio Esteban Tarrero
  4. # Proyecto ejecutado en CentOS 7 con interfaz gráfica
  5.  
  6. # Variables
  7. DIRECTORIO=$1
  8. OPCION=$2
  9. OPCIONEXTRA=$3
  10. ACCION=$4
  11.  
  12. # Usamos una flag para que nos haga un \n  entre los archivos que se sacan por pantalla
  13. let FLAG=0
  14.  
  15. #función principal
  16.  
  17. # MENÚ
  18. function menu ()
  19. {
  20.     echo -e "Forma de usar el programa: "
  21.     echo -e " <directorio> <opción de búsqueda> <opción extra> <acción> \n"
  22.     echo -e "Ejemplo: /home/sergio/downloads/pruebas -t -d -print"
  23.     echo -e "IMPORTANTE: Todos los comandos introducidos deben estar escritos en minúsculas"
  24. }
  25.  
  26. # LISTAR --> pone en una lista los ficheros
  27. function listar ()
  28. {
  29.     if [[ ${FLAG} = 1 ]]
  30.     then
  31.         lista+=(\n)
  32.     fi
  33.         lista+=${FICHERO}
  34. }
  35.  
  36. # Primera comprobación para ver si existe el directorio introducido
  37. if [[ ! -r ${DIRECTORIO} ]]
  38. then
  39.  
  40.     echo -e "ERROR: El directorio introducido no existe, pruebe otro \n"
  41.     menu # vuelve al menú principal
  42.    
  43. else
  44.  
  45.     # Switch Case de posibles elecciones de OPCIÓN
  46.  
  47.     case "${OPCION}" in
  48.      
  49.      "-t") # Caso buscar por TIPO DE ARCHIVO
  50.  
  51.         case "${OPCIONEXTRA}" in
  52.  
  53.             "-f") # Mira solo los ficheros normales, (Ej: .txt y fotos)
  54.                 for fichero in "${DIRECTORIO}"/*;
  55.                 do
  56.                     if [[ -r "${FICHERO}" && -f "${FICHERO}" ]]
  57.                     then
  58.                         listar
  59.                     fi
  60.                     done
  61.                 ;;
  62.  
  63.             "-d") # Mira solo los directorios
  64.                 for fichero in "${DIRECTORIO}"/*;
  65.                 do
  66.                     if [[ -r "${FICHERO}" && -d "${FICHERO}" ]]
  67.                     then
  68.                         listar
  69.                     fi
  70.                     done
  71.                 ;;
  72.  
  73.             "-") # Muestra todo
  74.                 for fichero in "${DIRECTORIO}"/*;
  75.                 do
  76.                   listar
  77.                 done
  78.                 ;;
  79.            
  80.             *) # Caso de error
  81.                echo -e "ERROR: Solo se pueden introducir los comandos -f, -d y -"
  82.                menu
  83.                ;;
  84.         esac
  85.         ;;
  86.  
  87.      "-n") # Caso buscar por NOMBRE DE ARCHIVO
  88.         for fichero in "${DIRECTORIO}"/*;
  89.         do
  90.           if [[ "${FICHERO}" == *"${OPCIONEXTRA}"* ]]
  91.           then
  92.             listar
  93.           fi
  94.         done
  95.         ;;
  96.  
  97.      "-p") # Caso buscar por PERMISOS DE ARCHIVO
  98.  
  99.         case "${OPCIONEXTRA}" in
  100.  
  101.             "-x") # Esribe que lea
  102.                 for fichero in "${DIRECTORIO}"/*;
  103.                 do
  104.                    if [[ -x "${FICHERO}" && -f "${FICHERO}" ]] || [[ -x "${FICHERO}" && -d "${FICHERO}" ]]
  105.                 then
  106.                     listar
  107.                 fi
  108.                 done
  109.                 ;;
  110.  
  111.             "-r") # Poder leerlo
  112.                 for fichero in "${DIRECTORIO}"/*;
  113.                 do
  114.                    if [[ -r "${FICHERO}" && -f "${FICHERO}" ]] || [[ -r "${FICHERO}" && -d "${FICHERO}" ]]
  115.                 then
  116.                     listar
  117.                 fi
  118.                 done
  119.                 ;;
  120.  
  121.             "-w") # Se puede escribir
  122.                 for fichero in "${DIRECTORIO}"/*;
  123.                 do
  124.                    if [[ -w "${FICHERO}" && -f "${FICHERO}" ]] || [[ -w "${FICHERO}" && -d "${FICHERO}" ]]
  125.                 then
  126.                     listar
  127.                 fi
  128.                 done
  129.                 ;;
  130.  
  131.             *) # Demás opciones
  132.                 echo -e "ERROR: Solo se pueden introducir los comandos -t, -n y -w"
  133.                 ;;
  134.  
  135.      "-c") # Caso buscar por TODO, hay que comprobarlo manualmente
  136.         for fichero in "${DIRECTORIO}"/*;
  137.         do
  138.           if [[ -r "${FICHERO}" && -f "${FICHERO}" && -s "${FICHERO}" ]]
  139.         then
  140.             grep -c "${OPCIONEXTRA}" "${FICHERO}" &>/dev/null
  141.  
  142.             if [[ $? -eq 0 ]]
  143.             then
  144.                 listar
  145.             fi
  146.           fi
  147.         done
  148.         ;;
  149.  
  150.      *) # Todo lo demás
  151.         echo -e "ERROR: Solo se pueden introducir los comandos -x, -r, -p y -c"
  152.         ;;
  153.  
  154.     esac
  155.      
  156.  case "${ACCION}" in
  157.  
  158.     "-print" | "") # Imprimir la búsqueda realizada
  159.  
  160.       shift # Shift mueve el nº de los argumentos
  161.       echo -e "${lista[@]}"
  162.       ;;
  163.  
  164.     "-exec") # Ejecutar el fichero después de buscarlo
  165.  
  166.       while [[ $# -gt 4 ]]
  167.       do
  168.         args+=${5}
  169.         shift # Shift mueve el nº de los argumentos
  170.       done
  171.  
  172.       echo -e "NO FUNCIONA"
  173.       ;;
  174.  
  175.     *) # Todas las demás opciones
  176.       echo -e "ERROR:ERROR: Solo se pueden introducir los comandos -print o -exec \n"
  177.       ;;
  178.  
  179.  esac
  180.  
  181. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement