Shell Script for file and directory localization

2

I would like someone to help me create this script correctly, in case it is to create a script that you type the name and directory of a file and then indicate if it was located or not and that generates this type of output on the screen

  • $ ./localizes.sh
  • Type the name of the file: .txt file
  • Enter the directory name: / home / user / Documents
  • The .txt file was not found in the directory / home / user / Documents
  • Or

  • $ ./localizes.sh
  • Type the name of the file: .txt file
  • Type the directory name: / home / user / Codes
  • The .txt file was located in the / home / user / Codes directory
  • I did but it was not completely correct

    #!/bin/bash
    
    # localiza.sh
    
    # script para localizar arquivos e diretórios
    
    echo "Digite o nome do diretório: "
    
    read DIR
    
    echo "Digite o nome do arquivo: "
    
    read ARQUIVO
    
    #
    
    find $DIR -name '$ARQUIVO' && echo "Busca efetuada com sucesso!" || echo "Arquivo não encontrado"
    
        
    asked by anonymous 13.04.2017 / 18:53

    1 answer

    1

    I made some modifications to your code and it worked here:

    #!/bin/bash
    
    # localiza.sh
    
    # script para localizar arquivos e diretórios
    
    echo "Digite o nome do diretório: "
    
    read DIR
    
    echo "Digite o nome do arquivo: "
    
    read ARQUIVO
    
    X=$(find $DIR -name "$ARQUIVO")
    
    #
    [ -n "$X" ] && echo "Busca efetuada com sucesso!" || echo "Arquivo não encontrado!"
    
        
    25.04.2017 / 21:20