Re: do shell script gotcha
Re: do shell script gotcha
- Subject: Re: do shell script gotcha
- From: Christopher Nebel <email@hidden>
- Date: Tue, 25 Feb 2003 02:14:05 -0800
On Monday, February 24, 2003, at 11:33 PM, Donald Hall wrote:
I recently discovered a change in AppleScript behavior in Jaguar that
I didn't see in the Release Notes for AS 1.9.
do shell script "ls myFile" returns an empty result if myFile can't be
found. In AS 1.8 it returns something like "No such file or directory
myFile". I was using the presence or absence of "No" in the result to
determine if the file was there or not. Of course this broke in
Jaguar, causing my script to give an "unexpected result". To cover
both 10.1 and 10.2, I now use:
set shellResult to do shell script "ls myFile"
if shellResult is "" or (offset of "No" in shellResult is not equal to
0) then -- file not found
This didn't change in 1.9, it changed in 1.8.2. The initial version of
"do shell script" had a rule that if a command had a zero exit status
(as ls does in this case), produced no output on stdout, and *did* have
output on stderr, then it would return stderr as the result. This was
deemed unnecessarily clever, and was changed so it always returned
stdout. For what it's worth, you're the first person I've heard of
this change biting.
Using ls to detect whether or not a file exists is awkward because of
how it handles missing files -- it always returns zero and spits out a
diagnostic message. Consider using something like this instead:
do shell script "test -f myFile && echo yes" equals "yes"
test(1) can do all sorts of things -- read the man page for details.
If you really want to stick with ls, you can get both stdout and stderr
together by adding "2>&1" to the end of the command, e.g. "ls myFile
2>&1".
--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.