javascript - CoffeeScript - Execute bash script with arguments - Stack Overflow

I am playing around with GitHub's Hubot, and I try to execute a bash script inside my robot work.

I am playing around with GitHub's Hubot, and I try to execute a bash script inside my robot work.
I succeed executing my script, but cannot get it working if I add some arguments to this script.

{ 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."

How do I pass arguments to my script ?
Thanks for help

I am playing around with GitHub's Hubot, and I try to execute a bash script inside my robot work.
I succeed executing my script, but cannot get it working if I add some arguments to this script.

{ 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."

How do I pass arguments to my script ?
Thanks for help

Share Improve this question asked Feb 17, 2014 at 13:50 eoutieouti 5,6583 gold badges35 silver badges42 bronze badges 1
  • You could say spawn "./myScript.sh #{url} #{title}" but that would leave you open to all sorts of unpleasant quoting and injection problems. Never ever use the single argument form of spawn, system, or similar "launch a shell to do things for me" mands, there's almost always a multi-argument form that bypasses the shell pletely. Aurélien's answer is the right way. – mu is too short Commented Feb 17, 2014 at 14:18
Add a ment  | 

1 Answer 1

Reset to default 8

As explained in the documentation:

http://nodejs/api/child_process.html#child_process_child_process_spawn_mand_args_options

Spawn take, as second parameter, an array made of your different arguments. It would look like this:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296867a4417301.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信