Do Command-Line Errors Work with "Try"? (ditto, specifically)

I just realized I had a “try” block with ditto in my script and that it was possible AppleScript wouldn’t catch any problems. Do shell scripts activated by AppleScript still trigger “try” properly if there is a problem?

If ditto encounters a problem moving a file (because of permissions, corrupt file, whatever), will that error be “seen” by AppleScript so that using try will work properly? So would this work?

(Basically, I have a script that copies specific files from one server to another, then deletes the file from the point of origin when done.)

try
	do shell script "ditto " & [i][supplied path names][/i]
on error errMsg
	[i][do some logging and make sure the files are not deleted from the source volume][/i]
end try

???

Hi,

shell scripts usually have a numeric return value (success = 0, failure ≠0) and a result written to stdout and/or an error message written to stderr which is the result of the do shell script line.

In AppleScript all numeric return values ≠0 are treated as errors, the numeric value is the error number and the returned string is the error message.

So just so I’m clear:

ditto by default is verbose enough that if it copies a folder full of files and subfolders and more files that it reports text in the Terminal window when a file won’t copy, but otherwise continues copying any items that were NOT problematic.

If I put a “do shell scipt ditto” in a “try” block, and ditto encounters 1 or more files it can’t copy succesfully, the try block should fire off the “on error” code?

Just want to be 100% sure about this before I switch-on my script on the live/production environment. :o

First some (hopefully) self explanatory examples:

-- doesn't return error because last command returns zero
do shell script "false;false;false;true"
-- returns an error because last command returns non-zero
do shell script "true;true;true;false"

-- send something to stderr and see what happens
-- when the last command returns zero the error messages are ignored
do shell script "echo 'error message' >&2 ;true"
-- only when the last command returns non-zero the error messages is showed
do shell script "echo 'error message' >&2 ;false"

To answer your question, this is what the manual says:

So when in the do shell script ditto is the only or last command in the script the try error block is safe.