Home > database >  Pass a variable containing a JSON as a parameter of a function by command line
Pass a variable containing a JSON as a parameter of a function by command line

Time:02-25

I need to enter a value in a JSON that requires an Openmediavault command. The command in question is this:

omv-rpc -u admin 'ShareMgmt' 'set'  '{"name":"120GB","mntentref":"71fdbd90-ce16-4726-ad8d-35ba8664b4c6","reldirpath": "/","mode": "775","comment": "","uuid": "fa4b1c66-ef79-11e5-87a0-0002b3a176b4"}'

But I need to introduce another JSON that I have saved in a variable. I have mounted it like this:

JSON_STRING=$( jq -n \
                  --arg referencia "$code_val" \
                  '{name:"120GB",mntentref:$referencia ,reldirpath: "/",mode: "775",comment: "",uuid: "fa4b1c66-ef79-11e5-87a0-0002b3a176b4"}')

The exit echo $JSON_STRING:

{"name":"120GB","mntentref":"71fdbd90-ce16-4726-ad8d-35ba8664b4c6","reldirpath": "/","mode": "775","comment": "","uuid": "fa4b1c66-ef79-11e5-87a0-0002b3a176b4"}

Now I want to build the first function and send the JSON that I have created by parameters, but I would not know how to do it, can you give me a hand?

I tried this way but it gives error. I have little knowledge of Debian console commands:

omv-rpc -u admin 'ShareMgmt' 'set' '{'echo $JSON_STRING'}'

CodePudding user response:

You need to use Shell Parameter Expansion to access variables:

omv-rpc -u admin 'ShareMgmt' 'set' "${JSON_STRING}"
  • Related