How to insert value of a query into a variable in Shell Script?

1

I have JSON in a file nomes.txt .

JSON is:

{"p": { "nome": ["josé","Maria", "carlos","Artur"] }}

I want to throw the query value of it into a variable.

Show the results:

#!/bin/bash

ns='cat nomes.txt'

echo $ns

for ((i=0; i<=4; i++))
do
        echo $ns | jq -r ".p | .nome[$i]"
done

But when I try to do something more elaborate I can not.

I want to play the value of the JSON caught in the variable but it will not.

#!/bin/bash

ns='cat nomes.txt'

echo $ns

for ((i=0; i<=4; i++))
do
        aux=$ns | jq -r ".p | .nome[$i]"
        echo "O nome $i é $aux"
done

The value for $aux is null.

How can I transfer the value to the aux variable?

    
asked by anonymous 05.09.2018 / 15:52

1 answer

0

I did a test and I believe that you solve this using the command encompassed by $()

So:

  aux=$($ns | jq -r ".p | .nome[$i]")

The test I did here was using the command $ls , like this:

ls='ls'
result=$($ls -la)
echo $result

In my result, it saved the directory listing in the result variable.

According to the response from SOEN , $() means "Run the command and put your output here"

I even asked a question to help with this question:

What is $ () the dollar sign followed by parentheses in BASH?

    
05.09.2018 / 16:35