2011-07-02 227 views
2

我正在处理一个Applescript,当它执行命令检查命令的结果后,我执行它,它会提示一条消息(在AppleScript编辑器中); Expected expression but found “error”.Applescript在执行shell脚本时返回“非零状态下退出的命令”

我该如何备份并检查命令是否与The command exited with a non-zero status.相同,以便我可以做其他事情,如果它与错误消息相同?

do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges 

if error = "The command exited with a non-zero status." then 
    display dialog "Returned zero" 
else if result = "The command exited with a non-zero status." then 
    display dialog "Returned zero" 
else if result = "" then 
    do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64" 
    display dialog "Memcached Started" 
else 
    do shell script "killall memcached" with administrator privileges 
    display dialog "Memcached Stopped" 
end if 

编辑:更新版本

set error to do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges 

if error = "The command exited with a non-zero status." then 
    do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64" 
    display dialog "Memcached Started" 
else 
    do shell script "killall memcached" with administrator privileges 
    display dialog "Memcached Stopped" 
end if 

回答

3

你的具体问题是在使用单词 “错误” 作为一个变量。错误是applescript的一个特殊词,因此它不能用作变量。

但是,即使您解决了这个问题,您的代码仍然无法正常工作。您用try块捕获错误消息。请注意,“theError”包含错误消息...

try 
    do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges 
on error theError 
    if theError is "The command exited with a non-zero status." then 
     do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64" 
     display dialog "Memcached Started" 
    else 
     do shell script "killall memcached" with administrator privileges 
     display dialog "Memcached Stopped" 
    end if 
end try 
相关问题