【问题标题】:do shell script results in command not found执行shell脚本导致找不到命令
【发布时间】:2017-12-02 16:31:26
【问题描述】:

我找到了用于 CPU Throttling 的 sh-script,并希望通过以下方式使用 AppleScript 在启动时安排它:

do shell script "sudo /Users/BohdanAir/Documents/Automation/cputhrottler.sh" "password mypassword" with administrator privileges

并得到以下错误:

error "sudo: /Users/BohdanAir/Documents/Automation/cputhrottler.sh: command not found" number 1

P.S: 提供给sh 文件的通行证是从Get Info 选项(CDM + I)完全复制的

【问题讨论】:

  • 不要将sudowith administrator privileges 一起使用 请参阅TechNote 2065。删除sudo
  • 虽然 varian 提出了很好的观点,但它出错的主要原因是您试图执行一个未设置为可执行的脚本,或者作为执行 shell 的直接参数执行.详情见我的回答。

标签: terminal applescript sh


【解决方案1】:

当您的问题中显示的 do shell script command 甚至无法编译时,甚至不确定您是如何收到该错误消息的。

"password mypassword" 部分实际上必须是 password "mypassword" 才能编译。

你得到的原因:

error "sudo: /Users/BohdanAir/Documents/Automation/cputhrottler.sh: command not found" number 1

是因为cputhrottler.sh 未设置为可执行文件。

使用以下命令使其可执行:

chmod u+x /Users/BohdanAir/Documents/Automation/cputhrottler.sh

这将允许它执行而不抛出特定的错误消息。

或者使用以下约定:

do shell script "sh /Users/BohdanAir/Documents/Automation/cputhrottler.sh" password "mypassword" with administrator privileges

注意sudo 被替换为sh,或者使用其他shell。以这种方式,shell 脚本 不需要变成可执行的。

【讨论】:

  • 你是对的,它不是可执行状态,使用约定可以完美执行。非常感谢:)
  • @Bohdan Afanasyev,是的,要直接执行 shell 脚本,它必须是可执行的,否则它必须作为参数传递给 shell 本身的执行。两种方法都可以,只是个人喜好问题。就个人而言,我更喜欢让我的 shell 脚本直接可执行,而不是作为参数传递给 shell 的执行。
猜你喜欢
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 2011-11-13
相关资源
最近更新 更多