do shell script - get standard output into a variable when error

I have a do shell script which can produce errors. I need to ignore the errors so I have put it into a try/end try block. But, I need to get the standard output into a variable when there is an error (and when not). This is the code I have which does work - result goes into get_result which I use elsewhere in the script:

try
	set get_result to do shell script "[my shell command] 2>&1"
	on error errStr
		set get_result to errStr
end try

It seems odd that if there is an error in the shell command, standard output is not normally available to AppleScript unless it is combined with standard error. Is there a better way of doing this ?

Thanks.

Hi.

I don’t think so. do shell script simply creates a Unix shell and sends it code to execute, getting back a result (if any) and a number indicating whether or not the code executed successfully. What you’ve done seems to be exactly in accordance with this explanation in Apple’s Technical Note on using do shell script.

Would it be possible to accomplish what Neophyte wants by redirecting standard error to /dev/null? Something like the following:


try
	set get_result to do shell script "[my shell command] 2>/dev/null "
on error errStr
	set get_result to errStr
end try

Should do. I’ll try it out.

Many thanks.