简体   繁体   中英

Why jq does not see environment variables when run in script?

I have the following JSON file:

{
"1":
 {
   "media_content":"test3.xspf"
 },
"2":
 {
   "media_content":"test3.xspf"
 }
}

In the terminal, using bash as shell, I can execute the following commands:

export schedules="1"
echo $(jq '.[env.schedules]["media_content"]' json_file.json)

Which results in outputing this:

test3.xspf

So it works as expected, but when I place that jq command in a script and run it, it just returns null. I did echo the values of schedules to make sure the value is non-null inside the script, and it is ok:

echo $schedules

But I did not manage to find the reason, why this command works when run directly in shell and does not work when run in script.

I run the script in the following ways:

bash script.sh
./script.sh

PS: yes, I did offer execute permission: chmod +x script.sh

HINT: env.schedules represents the environment variable 'schedules', and I did make sure that it is assigned in the script before calling jq .

EDIT: I am posting now a whole script, specifying the files tree.

There is one directory containing:

  • script.sh
  • json_file.json
  • static.json

script.sh:

export zone=$(cat static.json | jq '.["1"]');

echo "json block: "$zone

export schedules="$(echo $zone | jq '.schedules')"

echo "environment variable: "$schedules
export media_content=$(jq '.[env.schedules]["media_content"]' json_file.json)

echo "What I want to get: \"test3.xspf\""
echo "What I get: "$media_content

json_file.json:

{
"1":
 {
   "media_content":"test3.xspf"
 },
"2":
 {
   "media_content":"test3.xspf"
 }
}

static.json:

{
"1":
 {
   "x": "0",
   "y": "0",
   "width": "960",
   "height": "540",
   "schedules":"1"
 }
}

If I run the script, it displays:

json block: { "x": "0", "y": "0", "width": "960", "height": "540", "schedules": "1" }
environment variable: "1"
What I want to get: "test3.xspf"
What I get: null

If I hardcode the variable:

export schedules="1"   

The problem no longer occurs

The problem is simple.
It's not jq's fault.
It the unproper way the schedule's value is piped to the next command.

You have to remove the "s that surround the variable's value, add the second command that uses sed to do that:

    export schedules="$(echo $zone | jq '.schedules')"
    schedules=$( echo $schedules | sed s/\"//g )

Long answer Let's see:
here schedules is a string and echo shows its value as being 1:

export schedules="1" ; echo $schedules 

here even though double quotes are not mentioned:

export schedules=1 ; echo $schedules 

But the result from this also generates additional "s:

export schedules=$(echo $zone | jq '.schedules')

If you print it now you will see additional "s:

echo $schedules # "1"

So just remove the "s from the value:

schedules=$( echo $schedules | sed s/\"//g )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM