I am creating a script in Ubuntu and I can not make the script before checking the md5, it tells me if the files are accessible to the current user.
The script must generate the md5 of directories or files, and I would like it before generating it, the script should check if all the files in the directory and subdirectories are accessible to the current user. If it is not accessible, the script must request the user to execute it as root and finish its execution.
SOLVED - UPDATE:
Finally I managed to make it work. I tried if the md5sum returned error or not and there I discovered that it was returning error. Obviously, since no error occurred with find
, no error code returned. I found this: Can I make find non-return when no matching files were found? and helped me solve it by replacing \;
by +
at the end of find
.
On the other hand 2> /dev/null
prevents the Permission Denied error from being printed and the if [ "$?" != "0" ]
checks if the return code is not equal to 0.
Looking in the find manual, I have not understood why it does this because it specifies that adding +
will always return true
. As you can see in this example that I publish, adding +
, returns the return of -exec
.
Here I publish the example with the problem solved:
#!/bin/bash clear tempdirectory=/tmp echo echo "Escriba la carpeta o archivo que desea comprobar:" echo read input echo find "$input" -type f -exec md5sum {} + 2> /dev/null > $tempdirectory/archive.txt if [ "$?" != "0" ]; then echo echo "Se han encontrado errores durante la lectura." echo else echo echo "No se han encontrado errores durante la lectura." echo fi exit