node.js - 节点子进程bash脚本中的嵌套json参数
问题描述
我正在尝试使用 node 执行一个 bash 命令,该命令将嵌套的 json 对象作为参数。我发现嵌套的 json 不起作用。
例如尝试转换此 bash 命令。注意awscloudformation
是一个嵌套对象。
#!/bin/bash
set -e
IFS='|'
AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"testing\"\
}"
PROVIDERS="{\
\"awscloudformation\":$AWSCLOUDFORMATIONCONFIG\
}"
amplify init \
--providers $PROVIDERS \
这是无法按预期工作的节点脚本。Providers 似乎被忽略了,并且没有像上面的 bash 脚本那样覆盖默认值。
const arguments = [
'--providers',
`{"awscloudformation":{"configLevel":"project","useProfile":true,"profileName":"testing"}}`,
];
const opts = { stdio: 'inherit', shell: true };
require('child_process').spawn('amplify init', getArgs(), opts);
会exec
更适合这里吗?看来我需要包含一个类似于$AWSCLOUDFORMATIONCONFIG
在 bash 脚本中使用的变量。
有趣的是,其他未嵌套的参数似乎工作正常。例如这个放大参数工作正常:
'--amplify',
'{"projectName":"checking987","envName":"dev","defaultEditor":"code"}',
解决方案
最好在shell: false
这里使用:
const arguments = [
'--providers',
`{"awscloudformation":{"configLevel":"project","useProfile":true,"profileName":"testing"}}`,
];
const opts = { stdio: 'inherit', shell: false };
require('child_process').spawn("amplify", ["init", ...arguments], opts);
在 shell 模式下,双引号被删除。
改编的 shell 模式版本有效:
const arguments = [
'--providers',
`{\\"awscloudformation\\":{\\"configLevel\\":\\"project\\",\\"useProfile\\":true,\\"profileName\\":\\"testing\\"}}`,
];
const opts = { stdio: 'inherit', shell: true };
require('child_process').spawn("amplify init", arguments, opts);
推荐阅读
- java - 如何解决 NotOfficeXmlFileException
- reactjs - 如何将状态附加到数组
- swift - 在 Swift 中为 iOS 应用程序实现 Facebook Audien Network 广告
- reactjs - 为什么 AsyncTypeahead 不呈现我的选项?
- java - Java - 将数组元素与传递的 int 进行比较以查看是否相同时,if 语句失败
- python-3.x - Python3在有互联网连接时运行功能+每x分钟检查一次连接
- java - 如何将 json 文本文件解析为 BasicDBobject
- flutter - 如何更改 Flutter 中的默认字体系列
- javascript - JS 惯用的 alert() 没有搞砸 HTML 渲染
- java - 使用 Docx4j 从 XML 转换为 Docx