简体   繁体   中英

Pipe variable from a Bash file into a JQ command in another Bash file

Goal

Pipe variable from a Bash file into a JQ command in another Bash file

Context

I have a JSON file with two empty values I want to populate with:

  • An HTML file
  • A variable in a Bash file (file-b.sh)

The HTML portion works. I cannot figure out the variable portion. I show the HTML portion to demonstrate the logic I'm attempting to follow.

file-a.sh

#!/bin/bash

#This line (for the HTML portion) works:
jq --arg text "$(<file.html)" '.content.text=($text | @html)' my-json.json > file.json

# This line (for the Bash variable) doesn't...see end of question for result:
jq --arg variable "$subject" '.subject=$variable' file.json > file-1.json

file-b.sh

#!/bin/bash

subject="abc"
export subject
./file-a.sh

file-1.json: The result

The result is that content.text is populated, while subject is printed literally as the variable I attempted to code.

{
  "content": {
    "text": "Is populated; works great"
  },
  "subject": "$subject",
}

Based on what I have read, I have tried many iterations of single quotes, double quotes, parentheses, curly brackets (eg, {"$subject"} or ".subject=$variable" ), echo in a pipe, but continue to fail. Thank you for any ideas.

It seems to me that you don't need two bash scripts. You can call file-a.sh with an argument:

./file-a.sh "This is my subject" 

and within file-a.sh you can access the argument as:

echo "Subject: $1"

... where $1 , $2 etc are the variables

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