2011-07-02 266 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 
相關問題