Applescript 返回“命令以非零状态退出”执行 shell 脚本

发布于 2024-11-18 08:11:28 字数 1260 浏览 0 评论 0原文

我正在使用一个我试图制作的Applescript,在它执行命令后检查命令的结果,但是当我执行它时,它会提示一条消息(在AppleScript编辑器中); 预期的表达,但发现“错误”。

如何回退并检查该命令是否与 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

I'm working with an Applescript that I'm trying to make, after it does a command to check back results of a command, but when I execute it, it prompts a message (in AppleScript Editor); Expected expression but found “error”..

How can I fallback and check if the command is same as The command exited with a non-zero status. so I can do something else if it's the same as the error message?

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

EDIT: Updated version

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-11-25 08:11:28

您的特殊问题是使用“错误”一词作为变量。 Error 对于 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

Your particular problem is in using the word "error" as a variable. Error is a special word to applescript so it can't be used as a variable.

However, even if you fix that problem your code still won't work properly. You catch error messages with a try block. Note that "theError" contains the error message...

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文