Build a JSON string with Bash variables

后端 未结 12 1399
感情败类
感情败类 2020-11-29 01:57

I need to read these bash variables into my JSON string and I am not familiar with bash. any help is appreciated.

#!/bin/sh

BUCKET_NAME=testbucket
OBJECT_N         


        
12条回答
  •  眼角桃花
    2020-11-29 02:52

    These solutions come a little late but I think they are inherently simpler that previous suggestions (avoiding the complications of quoting and escaping).

        BUCKET_NAME=testbucket
        OBJECT_NAME=testworkflow-2.0.1.jar
        TARGET_LOCATION=/opt/test/testworkflow-2.0.1.jar
        
        # Initial unsuccessful solution
        JSON_STRING='{"bucketname":"$BUCKET_NAME","objectname":"$OBJECT_NAME","targetlocation":"$TARGET_LOCATION"}'
        echo $JSON_STRING 
        
        # If your substitution variables have NO whitespace this is sufficient
        JSON_STRING=$(tr -d [:space:] <

提交回复
热议问题