0

I've been working on a little script that will take data from a json file and will make it into a markdown template. The reading from the files works fine and reading the JSON data. When I print the variables separately it works with no problem. The script creates one file named 'passive-.md' but none of the variables ate placed within the template and it doesn't create a new template for each file it iterates over in the for loop.


#!/bin/bash

for file in *.txt;
do
    ip= jq  '.ip_str' $file

    ports= jq  '.ports' $file
    os= jq  '.os' $file
    domains= jq  '.domains' $file

    vulns= jq  '.vulns' $file
    isp= jq  '.isp' $file

    hostname= jq  '.hostname' $file
    echo "$ip $ports $os $domains $vulns $isp $hostname" # debug to check the values works
    output='# IP address ${ip}
#domains: ${domain}
## Hostname: ${hostname}
## Operating system: ${os}
## ISP: ${isp}
# ports ${ports}

#vulns


\~~~
${vulns}

\~~~

## Notes

'
    echo "${output}"  > ./output/passive-${ip}.md
done



t7086141
  • 9
  • 2
  • 7
  • The problems here are not specific to jq; they're misunderstandings common to "how do I store output from a program in a variable?" in general. `output= program arg1 arg2` doesn't persistently modify `output` at all, nor does it capture the stdout from `program`; you want `output=$(program arg1 arg2)`. – Charles Duffy Jun 16 '22 at 12:23
  • ...that said, it would be **far** more efficient if you called `jq` only once, and made it responsible for generating your output document, instead of having jq spit variables to bash (starting a new copy of jq for every single variable) and then having bash assemble them into output as you do now. – Charles Duffy Jun 16 '22 at 12:24
  • ...even if you didn't want to go that far, it would still be a lot more efficient to call jq only once per file. `eval "$(jq -r '"ip=\(.ip_str|@sh); os=\(.os|@sh)"')"` and then you set both `ip` and `os` after only one copy of jq is started. – Charles Duffy Jun 16 '22 at 12:29
  • Thanks, I didn't know you could do that. – t7086141 Jun 16 '22 at 12:36
  • Also, note that a single-quoted string won't perform parameter expansions, so `output='${foo}'` stores the exact string `${foo}`, not the _content of the variable_ `$foo`. It needs to be double quotes or a heredoc for expansions to be performed. – Charles Duffy Jun 16 '22 at 14:36

0 Answers0