Cancel find before the first file with permission denied












2















I tried to do this, but the only way I had was using the return code; this has the disadvantage that in my case, as I must generate the md5 signatures, it becomes eternal and everything to tell me that permission was denied.



For this reason I would like to do something more efficient. That before the first permission denied, find cancel the search.



The command in Bash (Ubuntu) that I currently use is:



find "$input" -type f -exec md5sum {} + 2> /dev/null > "$tempdirectory/archive.txt"
if [ "$?" != "0" ]; then
echo
echo "Permission denied in file(s)"
echo
fi
exit









share|improve this question























  • is this what you're looking for? unix.stackexchange.com/questions/62880/…

    – NEGR KITAEC
    Jan 2 at 13:43











  • @NEGRKITAEC It does not seem to be what I'm looking for. I have tried as they say in the answers, adding head -n 1 but this makes that instead of checking the md5 they find find: "md5sum" terminated by signal 13 and the program does not finish until all the files are scanned. So, instead of getting the md5, I just get a list of messages. The script that I publish does what I need, but only that it does it when the program finishes and I need that it does during the execution of find. Try the other examples and I have not obtained this result.

    – MarianoM
    Jan 2 at 14:06











  • find could still return valid results after the first permission denied. I don't think it's a good idea to cancel the find.

    – max
    Jan 2 at 14:55






  • 1





    @max I understand that in some cases I do not, but my script has a specific purpose that I am still testing and I need this function. The idea of ​​canceling when finding denied permission is to warn the user that he must execute root to continue and read all the files. Or you could also ask him if he wants to continue and only take the md5 from those that can be read.

    – MarianoM
    Jan 2 at 15:01
















2















I tried to do this, but the only way I had was using the return code; this has the disadvantage that in my case, as I must generate the md5 signatures, it becomes eternal and everything to tell me that permission was denied.



For this reason I would like to do something more efficient. That before the first permission denied, find cancel the search.



The command in Bash (Ubuntu) that I currently use is:



find "$input" -type f -exec md5sum {} + 2> /dev/null > "$tempdirectory/archive.txt"
if [ "$?" != "0" ]; then
echo
echo "Permission denied in file(s)"
echo
fi
exit









share|improve this question























  • is this what you're looking for? unix.stackexchange.com/questions/62880/…

    – NEGR KITAEC
    Jan 2 at 13:43











  • @NEGRKITAEC It does not seem to be what I'm looking for. I have tried as they say in the answers, adding head -n 1 but this makes that instead of checking the md5 they find find: "md5sum" terminated by signal 13 and the program does not finish until all the files are scanned. So, instead of getting the md5, I just get a list of messages. The script that I publish does what I need, but only that it does it when the program finishes and I need that it does during the execution of find. Try the other examples and I have not obtained this result.

    – MarianoM
    Jan 2 at 14:06











  • find could still return valid results after the first permission denied. I don't think it's a good idea to cancel the find.

    – max
    Jan 2 at 14:55






  • 1





    @max I understand that in some cases I do not, but my script has a specific purpose that I am still testing and I need this function. The idea of ​​canceling when finding denied permission is to warn the user that he must execute root to continue and read all the files. Or you could also ask him if he wants to continue and only take the md5 from those that can be read.

    – MarianoM
    Jan 2 at 15:01














2












2








2


1






I tried to do this, but the only way I had was using the return code; this has the disadvantage that in my case, as I must generate the md5 signatures, it becomes eternal and everything to tell me that permission was denied.



For this reason I would like to do something more efficient. That before the first permission denied, find cancel the search.



The command in Bash (Ubuntu) that I currently use is:



find "$input" -type f -exec md5sum {} + 2> /dev/null > "$tempdirectory/archive.txt"
if [ "$?" != "0" ]; then
echo
echo "Permission denied in file(s)"
echo
fi
exit









share|improve this question














I tried to do this, but the only way I had was using the return code; this has the disadvantage that in my case, as I must generate the md5 signatures, it becomes eternal and everything to tell me that permission was denied.



For this reason I would like to do something more efficient. That before the first permission denied, find cancel the search.



The command in Bash (Ubuntu) that I currently use is:



find "$input" -type f -exec md5sum {} + 2> /dev/null > "$tempdirectory/archive.txt"
if [ "$?" != "0" ]; then
echo
echo "Permission denied in file(s)"
echo
fi
exit






bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 13:36









MarianoMMarianoM

132




132













  • is this what you're looking for? unix.stackexchange.com/questions/62880/…

    – NEGR KITAEC
    Jan 2 at 13:43











  • @NEGRKITAEC It does not seem to be what I'm looking for. I have tried as they say in the answers, adding head -n 1 but this makes that instead of checking the md5 they find find: "md5sum" terminated by signal 13 and the program does not finish until all the files are scanned. So, instead of getting the md5, I just get a list of messages. The script that I publish does what I need, but only that it does it when the program finishes and I need that it does during the execution of find. Try the other examples and I have not obtained this result.

    – MarianoM
    Jan 2 at 14:06











  • find could still return valid results after the first permission denied. I don't think it's a good idea to cancel the find.

    – max
    Jan 2 at 14:55






  • 1





    @max I understand that in some cases I do not, but my script has a specific purpose that I am still testing and I need this function. The idea of ​​canceling when finding denied permission is to warn the user that he must execute root to continue and read all the files. Or you could also ask him if he wants to continue and only take the md5 from those that can be read.

    – MarianoM
    Jan 2 at 15:01



















  • is this what you're looking for? unix.stackexchange.com/questions/62880/…

    – NEGR KITAEC
    Jan 2 at 13:43











  • @NEGRKITAEC It does not seem to be what I'm looking for. I have tried as they say in the answers, adding head -n 1 but this makes that instead of checking the md5 they find find: "md5sum" terminated by signal 13 and the program does not finish until all the files are scanned. So, instead of getting the md5, I just get a list of messages. The script that I publish does what I need, but only that it does it when the program finishes and I need that it does during the execution of find. Try the other examples and I have not obtained this result.

    – MarianoM
    Jan 2 at 14:06











  • find could still return valid results after the first permission denied. I don't think it's a good idea to cancel the find.

    – max
    Jan 2 at 14:55






  • 1





    @max I understand that in some cases I do not, but my script has a specific purpose that I am still testing and I need this function. The idea of ​​canceling when finding denied permission is to warn the user that he must execute root to continue and read all the files. Or you could also ask him if he wants to continue and only take the md5 from those that can be read.

    – MarianoM
    Jan 2 at 15:01

















is this what you're looking for? unix.stackexchange.com/questions/62880/…

– NEGR KITAEC
Jan 2 at 13:43





is this what you're looking for? unix.stackexchange.com/questions/62880/…

– NEGR KITAEC
Jan 2 at 13:43













@NEGRKITAEC It does not seem to be what I'm looking for. I have tried as they say in the answers, adding head -n 1 but this makes that instead of checking the md5 they find find: "md5sum" terminated by signal 13 and the program does not finish until all the files are scanned. So, instead of getting the md5, I just get a list of messages. The script that I publish does what I need, but only that it does it when the program finishes and I need that it does during the execution of find. Try the other examples and I have not obtained this result.

– MarianoM
Jan 2 at 14:06





@NEGRKITAEC It does not seem to be what I'm looking for. I have tried as they say in the answers, adding head -n 1 but this makes that instead of checking the md5 they find find: "md5sum" terminated by signal 13 and the program does not finish until all the files are scanned. So, instead of getting the md5, I just get a list of messages. The script that I publish does what I need, but only that it does it when the program finishes and I need that it does during the execution of find. Try the other examples and I have not obtained this result.

– MarianoM
Jan 2 at 14:06













find could still return valid results after the first permission denied. I don't think it's a good idea to cancel the find.

– max
Jan 2 at 14:55





find could still return valid results after the first permission denied. I don't think it's a good idea to cancel the find.

– max
Jan 2 at 14:55




1




1





@max I understand that in some cases I do not, but my script has a specific purpose that I am still testing and I need this function. The idea of ​​canceling when finding denied permission is to warn the user that he must execute root to continue and read all the files. Or you could also ask him if he wants to continue and only take the md5 from those that can be read.

– MarianoM
Jan 2 at 15:01





@max I understand that in some cases I do not, but my script has a specific purpose that I am still testing and I need this function. The idea of ​​canceling when finding denied permission is to warn the user that he must execute root to continue and read all the files. Or you could also ask him if he wants to continue and only take the md5 from those that can be read.

– MarianoM
Jan 2 at 15:01












2 Answers
2






active

oldest

votes


















0














Assuming you're using GNU find, I would use the following :



find "$input" ! -readable -fprintf /dev/stderr '%p cannot be read ! Switch to root.n' -a -quit -o -type f -exec md5sum {} + > "$tempdirectory/archive.txt"


As soon as it finds an unreadable file or directory, it outputs a message on stderr mentionning the unreadable file then aborts. Until this happens, it aggregates file names to pass them to md5sum whose output (on stdout) will be redirected to the archive.txt file.



From my tests it seems like the solution isn't perfect either because -quit won't quit immediately but run the -exec [...] + that was in construction when it was reached (find's manual mentions that for -execdir [...] +, but I guess it extends to -exec). You could avoid that by using -exec [...] ; instead, but the impact on nominal performances would likely be huge.



It relies on the -readable predicate to determine whether the file can be read by your user, otherwise it runs the -fprintf and -quit actions to first print an error message to stderr (so that it won't be redirected to your output file) and then abort the search.



You can try it here.






share|improve this answer


























  • Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

    – MarianoM
    Jan 2 at 16:02











  • @MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

    – Aaron
    Jan 2 at 16:37













  • Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

    – MarianoM
    Jan 2 at 17:50











  • @MarianoM Glad to hear it, happy 2019 to you too !

    – Aaron
    Jan 2 at 17:58



















1














Have you try adding "set -e" to the beginning of your script?




set -e: Exit immediately if a command exits with a non-zero status.




set -e was an attempt to add “automatic error detection” to the shell.

Its goal was to cause the shell to abort any time an error occurred, so we don’t have to put || exit 1 after each important command.

But it’s not very reliable and considered as a bad practice.



trap is a better alternative and its proper implementation works with any level of function nesting, any flow.



#!/bin/bash
trap 'echo "failed with exitcode $?" 1>&2' ERR

# your "find" code here


Since "permission denied" has a return status of "1", it can be used to cancel the search.

You can read more about trap right here






share|improve this answer


























  • Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

    – MarianoM
    Jan 2 at 15:32













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007361%2fcancel-find-before-the-first-file-with-permission-denied%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Assuming you're using GNU find, I would use the following :



find "$input" ! -readable -fprintf /dev/stderr '%p cannot be read ! Switch to root.n' -a -quit -o -type f -exec md5sum {} + > "$tempdirectory/archive.txt"


As soon as it finds an unreadable file or directory, it outputs a message on stderr mentionning the unreadable file then aborts. Until this happens, it aggregates file names to pass them to md5sum whose output (on stdout) will be redirected to the archive.txt file.



From my tests it seems like the solution isn't perfect either because -quit won't quit immediately but run the -exec [...] + that was in construction when it was reached (find's manual mentions that for -execdir [...] +, but I guess it extends to -exec). You could avoid that by using -exec [...] ; instead, but the impact on nominal performances would likely be huge.



It relies on the -readable predicate to determine whether the file can be read by your user, otherwise it runs the -fprintf and -quit actions to first print an error message to stderr (so that it won't be redirected to your output file) and then abort the search.



You can try it here.






share|improve this answer


























  • Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

    – MarianoM
    Jan 2 at 16:02











  • @MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

    – Aaron
    Jan 2 at 16:37













  • Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

    – MarianoM
    Jan 2 at 17:50











  • @MarianoM Glad to hear it, happy 2019 to you too !

    – Aaron
    Jan 2 at 17:58
















0














Assuming you're using GNU find, I would use the following :



find "$input" ! -readable -fprintf /dev/stderr '%p cannot be read ! Switch to root.n' -a -quit -o -type f -exec md5sum {} + > "$tempdirectory/archive.txt"


As soon as it finds an unreadable file or directory, it outputs a message on stderr mentionning the unreadable file then aborts. Until this happens, it aggregates file names to pass them to md5sum whose output (on stdout) will be redirected to the archive.txt file.



From my tests it seems like the solution isn't perfect either because -quit won't quit immediately but run the -exec [...] + that was in construction when it was reached (find's manual mentions that for -execdir [...] +, but I guess it extends to -exec). You could avoid that by using -exec [...] ; instead, but the impact on nominal performances would likely be huge.



It relies on the -readable predicate to determine whether the file can be read by your user, otherwise it runs the -fprintf and -quit actions to first print an error message to stderr (so that it won't be redirected to your output file) and then abort the search.



You can try it here.






share|improve this answer


























  • Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

    – MarianoM
    Jan 2 at 16:02











  • @MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

    – Aaron
    Jan 2 at 16:37













  • Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

    – MarianoM
    Jan 2 at 17:50











  • @MarianoM Glad to hear it, happy 2019 to you too !

    – Aaron
    Jan 2 at 17:58














0












0








0







Assuming you're using GNU find, I would use the following :



find "$input" ! -readable -fprintf /dev/stderr '%p cannot be read ! Switch to root.n' -a -quit -o -type f -exec md5sum {} + > "$tempdirectory/archive.txt"


As soon as it finds an unreadable file or directory, it outputs a message on stderr mentionning the unreadable file then aborts. Until this happens, it aggregates file names to pass them to md5sum whose output (on stdout) will be redirected to the archive.txt file.



From my tests it seems like the solution isn't perfect either because -quit won't quit immediately but run the -exec [...] + that was in construction when it was reached (find's manual mentions that for -execdir [...] +, but I guess it extends to -exec). You could avoid that by using -exec [...] ; instead, but the impact on nominal performances would likely be huge.



It relies on the -readable predicate to determine whether the file can be read by your user, otherwise it runs the -fprintf and -quit actions to first print an error message to stderr (so that it won't be redirected to your output file) and then abort the search.



You can try it here.






share|improve this answer















Assuming you're using GNU find, I would use the following :



find "$input" ! -readable -fprintf /dev/stderr '%p cannot be read ! Switch to root.n' -a -quit -o -type f -exec md5sum {} + > "$tempdirectory/archive.txt"


As soon as it finds an unreadable file or directory, it outputs a message on stderr mentionning the unreadable file then aborts. Until this happens, it aggregates file names to pass them to md5sum whose output (on stdout) will be redirected to the archive.txt file.



From my tests it seems like the solution isn't perfect either because -quit won't quit immediately but run the -exec [...] + that was in construction when it was reached (find's manual mentions that for -execdir [...] +, but I guess it extends to -exec). You could avoid that by using -exec [...] ; instead, but the impact on nominal performances would likely be huge.



It relies on the -readable predicate to determine whether the file can be read by your user, otherwise it runs the -fprintf and -quit actions to first print an error message to stderr (so that it won't be redirected to your output file) and then abort the search.



You can try it here.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 16:37

























answered Jan 2 at 15:26









AaronAaron

16.1k11636




16.1k11636













  • Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

    – MarianoM
    Jan 2 at 16:02











  • @MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

    – Aaron
    Jan 2 at 16:37













  • Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

    – MarianoM
    Jan 2 at 17:50











  • @MarianoM Glad to hear it, happy 2019 to you too !

    – Aaron
    Jan 2 at 17:58



















  • Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

    – MarianoM
    Jan 2 at 16:02











  • @MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

    – Aaron
    Jan 2 at 16:37













  • Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

    – MarianoM
    Jan 2 at 17:50











  • @MarianoM Glad to hear it, happy 2019 to you too !

    – Aaron
    Jan 2 at 17:58

















Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

– MarianoM
Jan 2 at 16:02





Yes, yes, yes, this is what I need. It works very well, only it is strange that in the root directory, it does not work as it should. I was testing it and should request root but it appears on the screen: find: «/home/ubuntu/.dbus»: Permission Denied. Can this happen with other files in other directories as well? In those that I have tried it has not failed so far. When I have time, could you better explain the operation of your command?

– MarianoM
Jan 2 at 16:02













@MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

– Aaron
Jan 2 at 16:37







@MarianoM Ah, I get it : .dbus is a directory rather than a file, and find is trying to descend into it, which it can't because it is owned by root. The error message isn't the specific one because I only raised it for unreadable files. I've updated my answer to handle that.

– Aaron
Jan 2 at 16:37















Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

– MarianoM
Jan 2 at 17:50





Thank you very much for everything Aaron! Now everything works very well as expected. I understand the -quit it does not close immediately, but I do not worry too much because it does what I expected. Have a happy 2019.

– MarianoM
Jan 2 at 17:50













@MarianoM Glad to hear it, happy 2019 to you too !

– Aaron
Jan 2 at 17:58





@MarianoM Glad to hear it, happy 2019 to you too !

– Aaron
Jan 2 at 17:58













1














Have you try adding "set -e" to the beginning of your script?




set -e: Exit immediately if a command exits with a non-zero status.




set -e was an attempt to add “automatic error detection” to the shell.

Its goal was to cause the shell to abort any time an error occurred, so we don’t have to put || exit 1 after each important command.

But it’s not very reliable and considered as a bad practice.



trap is a better alternative and its proper implementation works with any level of function nesting, any flow.



#!/bin/bash
trap 'echo "failed with exitcode $?" 1>&2' ERR

# your "find" code here


Since "permission denied" has a return status of "1", it can be used to cancel the search.

You can read more about trap right here






share|improve this answer


























  • Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

    – MarianoM
    Jan 2 at 15:32


















1














Have you try adding "set -e" to the beginning of your script?




set -e: Exit immediately if a command exits with a non-zero status.




set -e was an attempt to add “automatic error detection” to the shell.

Its goal was to cause the shell to abort any time an error occurred, so we don’t have to put || exit 1 after each important command.

But it’s not very reliable and considered as a bad practice.



trap is a better alternative and its proper implementation works with any level of function nesting, any flow.



#!/bin/bash
trap 'echo "failed with exitcode $?" 1>&2' ERR

# your "find" code here


Since "permission denied" has a return status of "1", it can be used to cancel the search.

You can read more about trap right here






share|improve this answer


























  • Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

    – MarianoM
    Jan 2 at 15:32
















1












1








1







Have you try adding "set -e" to the beginning of your script?




set -e: Exit immediately if a command exits with a non-zero status.




set -e was an attempt to add “automatic error detection” to the shell.

Its goal was to cause the shell to abort any time an error occurred, so we don’t have to put || exit 1 after each important command.

But it’s not very reliable and considered as a bad practice.



trap is a better alternative and its proper implementation works with any level of function nesting, any flow.



#!/bin/bash
trap 'echo "failed with exitcode $?" 1>&2' ERR

# your "find" code here


Since "permission denied" has a return status of "1", it can be used to cancel the search.

You can read more about trap right here






share|improve this answer















Have you try adding "set -e" to the beginning of your script?




set -e: Exit immediately if a command exits with a non-zero status.




set -e was an attempt to add “automatic error detection” to the shell.

Its goal was to cause the shell to abort any time an error occurred, so we don’t have to put || exit 1 after each important command.

But it’s not very reliable and considered as a bad practice.



trap is a better alternative and its proper implementation works with any level of function nesting, any flow.



#!/bin/bash
trap 'echo "failed with exitcode $?" 1>&2' ERR

# your "find" code here


Since "permission denied" has a return status of "1", it can be used to cancel the search.

You can read more about trap right here







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 15:17

























answered Jan 2 at 14:58









py9py9

390110




390110













  • Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

    – MarianoM
    Jan 2 at 15:32





















  • Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

    – MarianoM
    Jan 2 at 15:32



















Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

– MarianoM
Jan 2 at 15:32







Good options and very functional, but it seems that they do the same function that I did in the script and it's not what I'm looking for. What I'm looking for is that the script immediately stops at the first permission denied error to warn the user that if he does not execute root, not all the files will be included. I thought I could redirect errors to a file or variable, but everything must wait for the find and md5 program to finish their function.

– MarianoM
Jan 2 at 15:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007361%2fcancel-find-before-the-first-file-with-permission-denied%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules