我正在嘗試使用 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"}',
uj5u.com熱心網友回復:
最好在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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323968.html
