【问题标题】:How to escape characters in a URL in a command used in child.process.exec in node如何在节点中 child.process.exec 中使用的命令中转义 URL 中的字符
【发布时间】:2018-04-19 12:56:37
【问题描述】:

我有一个可用于执行 Rscript 的有效命令,如下所示:

Rscript 02_Meta_Analyses.R --api_es 
http://localhost:8071/api/v1/studies/analysisData\?study\=00000000- 
0000-4000-0000-000000000000\&study\=00000000-0000-4000-0000- 
000000000001\&study\=00000000-0000-4200-0000-000000000000 --uuid 
STUDY_ID --author AUTHOR --year YEAR --type_1 GROUP1 --type_2 
GROUP2 --pedro PEDRO --api_select 
http://localhost:8071/api/v1/analysis/meta- 
analysis/5ad8505491951a966b6984fb --uuid_select STUDY_ID -- 
outbroad_select OUT_BROAD_SELECT

当我将它直接输入到我的 shell 时,这个脚本正在运行

但是,因为参数中的 uuid 是动态生成的。我创建命令字符串并在 child_procee.exec(command) 中使用它,如下所示:

// create the command.
const command = 'Rscript 02_Meta_Analyses.R --api_es 
http://localhost:8071/api/v1/studies/analysisData?study\=00000000- 
0000-4000-0000-000000000000&study=00000000-0000-4000-0000- 
000000000001&study=00000000-0000-4200-0000-000000000000 --uuid 
STUDY_ID --author AUTHOR --year YEAR --type_1 GROUP1 --type_2 
GROUP2 --pedro PEDRO --api_select 
http://localhost:8071/api/v1/analysis/meta- 
analysis/5ad8505491951a966b6984fb --uuid_select STUDY_ID -- 
outbroad_select OUT_BROAD_SELECT'

// execute the command.
const { exec } = require('child_process');
  exec(command, options, (error, stdout, stderr) => {
if (error) {
  pino.info(`Metadata analysis exec error: ${error}`);
  sendFailureEmail(user.email, done);
} else {
  ........  
}

我尽我所能:

例如 1

const command = 'Rscript 02_Meta_Analyses.R --api_es 
http://localhost:8071/api/v1/studies/analysisData\\?study\\=00000000- 
0000-4000-0000-000000000000\\&study\\=00000000-0000-4000-0000- 
000000000001\\&study\\=00000000-0000-4200-0000-000000000000 --uuid 
STUDY_ID --author AUTHOR --year YEAR --type_1 GROUP1 --type_2 
GROUP2 --pedro PEDRO --api_select 
http://localhost:8071/api/v1/analysis/meta- 
analysis/5ad8505491951a966b6984fb --uuid_select STUDY_ID -- 
outbroad_select OUT_BROAD_SELECT

例如 2:

const command = 'Rscript 02_Meta_Analyses.R --api_es 
"http://localhost:8071/api/v1/studies/analysisData\\? 
study\\=00000000- 
0000-4000-0000-000000000000\\&study\\=00000000-0000-4000-0000- 
000000000001\\&study\\=00000000-0000-4200-0000-000000000000" --uuid 
STUDY_ID --author AUTHOR --year YEAR --type_1 GROUP1 --type_2 
GROUP2 --pedro PEDRO --api_select 
http://localhost:8071/api/v1/analysis/meta- 
analysis/5ad8505491951a966b6984fb --uuid_select STUDY_ID -- 
outbroad_select OUT_BROAD_SELECT'

但似乎我无法获得可用于执行 R 脚本的有效命令。希望我能澄清我的问题,如果有任何提示,我将不胜感激。

【问题讨论】:

    标签: node.js linux bash child-process spawn


    【解决方案1】:

    有时候,最好的取胜方式就是不战斗。这与 shell 引用相得益彰。

    如果您将该命令写入脚本,您可以exec 该脚本仅使用 UUID 作为参数,避免需要转义所有内容。根据您的示例,这样的脚本如下所示:

    #!/bin/sh
    Rscript 02_Meta_Analyses.R --api_es "http://localhost:8071/api/v1/studies/analysisData?study=$1&study=$2&study=$3" \
     --uuid STUDY_ID --author AUTHOR --year YEAR --type_1 GROUP1 --type_2 GROUP2 \
     --pedro PEDRO --api_select "http://localhost:8071/api/v1/analysis/meta-analysis/$4" --uuid_select STUDY_ID --outbroad_select OUT_BROAD_SELECT
    

    如果你将该文件保存为magic-R-script,那么你可以像这样构建你的命令:

    const command = 'sh magic-R-script <UUID1> <UUID2> <UUID3> <UUID4>'
    

    然后所有的引用都由 shell 处理,让您避免一天的问题。

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2019-12-08
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      相关资源
      最近更新 更多