パラメータリストが長すぎる問題の回避策

パラメータリストが長すぎる問題の回避策

ファイルを読み込み、ファイルの内容を変数にコピーし、変数を別のコマンドに引数として渡す次のシェルスクリプトがあります。

declare -a arr=()
while IFS= read -r var
do
  arr+=( $var )
done < "accounts.json"
args=''
for j in "${arr[@]}"
 do
   args="$args $j"
 done
 peer chaincode invoke -n cc -C channel1 -c '{"Args":["InitLedgerAdvanced",'"\"$args\""']}'

この方法は account.json ファイルが小さい場合に有効です。しかし、account.jsonのサイズが大きすぎると、「パラメータリストが長すぎます」というエラーメッセージが表示されます。私は成功せずにxargsを試しました。

編集1:

以下は、2行だけを含むサンプルjsonファイルです。

[{"accountID":"C682227132","accountStatus":"1"},
{"accountID":"C800427392","accountStatus":"1"}]

実際のデータと同等のコマンドは次のとおりです。

peer chaincode invoke -n cc -C channel1 -c '{"Args":["InitLedgerAdvanced","[{"accountID":"C682227132","accountStatus":"1"},
{"accountID":"C800427392","accountStatus":"1"}]"]}'

ベストアンサー1

これ可能働く

# slurp the accounts file into a variable
accounts=$(< accounts.json)

# create the json, escaping the accounts quotes along the way
printf -v json '{"Args":["InitLedgerAdvanced","%s"]}' "${accounts//\"/\\\"}"

# and invoke the command
peer chaincode invoke -n cc -C channel1 -c "$json"

-cそれでも問題が発生した場合は、コマンドライン引数ではなく標準入力またはファイルを介して「ピア」に引数を渡す方法を見つける必要があります。

おすすめ記事