【问题标题】:How to pass parameter to shell script in Adonis JS?如何在 Adonis JS 中将参数传递给 shell 脚本?
【发布时间】:2020-07-20 06:20:27
【问题描述】:

我正在尝试从 post 请求运行 shell 脚本,我想将 post 请求中的参数传递给这个 shell 脚本。

const Helpers = use('Helpers')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const shellCommand = Helpers.appRoot('shellCommand.sh')
        
class someController {
  async generate({ request, response }) {
    let { VERSION } = request.body
      try {
           await exec(shellCommand + VERSION, function (error, stdout, stderr) {
              if (error !== null) {
                 console.log(error);
                 } else {
                   console.log('stdout: ' + stdout);
                   console.log('stderr: ' + stderr);
                 }
                })
            }
        catch (error) {
           console.log(error);
        }
     await response.send(JSON.stringify({ "status": 200, "message": "generate success", "version": version}))
            }
        
        }

这就是我尝试在我的 shell 命令中获取参数的方式。

VERSION = $1

如果我尝试运行该服务,这就是我得到的结果。它将参数作为文件名的补充传递。我知道这不是在 Adonis JS 中将参数传递给 shell 脚本的正确方法,我已经搜索了互联网但找不到这样做的方法。 error

【问题讨论】:

    标签: javascript shell parameter-passing child-process adonis.js


    【解决方案1】:

    Nodejs api 中所述,文件名和参数之间需要有空格,例如shellCommand + ' ' + VERSION。文档中也有说明,如果您使用 exec 的承诺版本,请不要提供回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2021-04-16
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多