I have a question about ignoring error from the “find” program in Bash. I am haveing a problem when searching the root level of a mounted volume.
If I use the following in Terminal I still get the error messgae but i also get the result afterwards.
find /volumes/myvolume -iname “Fred*”
Output: I get about 7 errors and then I get all the files with “Fred” afterwards.
find: /volumes/myvolume/.Spotlight-v100/,journalled historylog: Permission Denied. ← all spotlight permission errors
/volumes/myvolume/Fred.ca
When I use this with do shell script. It automatically errors out and won’t show me the result. Does anyone know how to get around this or am I just SOL.
Any help would be great,
Thanks Dallas
P.S. I can’t change the permissions on the volume.
You are falling afoul of an unfortunate confluence of design decisions in find and do shell script.
Sort answer: If you are confident that your find command will not have any errors other than permission errors, you can put “;true” on the end of your shell script command. When you do this, do shell script will never raise an error exception (even if there were legitimate, non-permissions, errors).
Not as short answer: If you are concerned that your find command might have other errors (for which you would like do shell script to raise its customary exception), you will have to resort to other techniques (ones fraught with problems of their own). My plan for this approach would be to use I/O redirection to merge the stderr output into stdout, filter out any innocuous output (like the permissions error messages) and then return an exit code based on the content of those error messages. This plan is incompatible with things like find’s -print0 command, which is almost always necessary to properly handle cases where filenames might contain some perfectly legal, but uncommon characters (specifically carriage return and linefeed when reading the output in AppleScript; also space and tab if piping to xargs; others if you are having the shell interpret the data).
If find used a unique exit code for ˜all errors were permissions problems’, then your shell script could check for that condition and override the exit code if and only if the only errors were permissions errors.
Or, if do shell script had a way to capture stderr output separately from stdout, you could check that output for errors other than permissions errors and do something appropriate. This can be done with some help on the shell script side by redirecting stderr to a file, saving the exit code to (a possibly distinct) file, always ending with true, then having AppleScript read the captured stderr and exit code to decide what to do.
As further to the discussion, I only need to worry about two errors. “Permission Denied” or “Volume Doesn’t exist”.
So I have chosen to go this route now that you have given me the answer to my problem.
set ext to quoted form of "PGR*"
try
set theresult to do shell script "find /volumes/myvolume -iname " & ext
on error err
if err contains "Permission denied" then
set theresult to do shell script "find /volumes/myvolume -iname " & ext & ";true"
return theresult
end if
if err does not contain "Permission denied" then
return err
end if
end try
Your script looks like it should work fine. However if the volume exists, it will end up using find to fully traverse it twice. If you find that this takes too much time, you could try something like this approach:
set volPP to "/volumes/myvolume"
set ext to quoted form of "PGR*"
tell application "Finder" to set volExists to exists (get AppleScript's POSIX file volPP)
if volExists then
set theresult to do shell script "find " & quoted form of volPP & " -iname " & ext & ";true"
return theresult
else
--error "Volume "" & volPP & "" does not exist"
end if
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)