【问题标题】:CoffeeScript - Execute bash script with argumentsCoffeeScript - 使用参数执行 bash 脚本
【发布时间】:2014-03-16 20:28:32
【问题描述】:

我正在玩 GitHub 的 Hubot,并尝试在我的机器人工作中执行一个 bash 脚本。
我成功执行了我的脚本,但如果我向这个脚本添加一些参数,它就无法正常工作。

{ spawn } = require 'child_process'
s = spawn './myScript.sh' + " url" + " title"     <------- doesn't work due to args
s = spawn './myScript.sh'                         <------- alright without args
s.stdout.on 'data', ( data ) -> console.log "Output: #{ data }"
s.stderr.on 'data', ( data ) -> console.error "Error: #{ data }"
s.on 'close', -> console.log "'s' has finished executing."

如何将参数传递给我的脚本?
感谢您的帮助

【问题讨论】:

  • 你可以说spawn "./myScript.sh #{url} #{title}",但这会让你面临各种令人不快的引用和注入问题。永远不要使用spawnsystem 或类似的“启动 shell 为我做事”命令的单参数形式,几乎总是有一个完全绕过 shell 的多参数形式。 Aurélien 的答案是正确的。

标签: javascript bash coffeescript hubot


【解决方案1】:

如文档中所述:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Spawn 将由您的不同参数组成的数组作为第二个参数。它看起来像这样:

s = spawn './myScript.sh', [url, title]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2011-10-16
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    相关资源
    最近更新 更多