bashcurljqtee

Redirect tee output to the variable instead of file


I'm trying to do the loop where output of each curl command which is returning json data is allocated to the variable/array. I'm using tee command to get my wanted value plus next token for next json piece of data. When I save data to file everything works fine but instead of saving data to file I would like to keep it in array/variable.

This works:

while  [[ "$next_token" != "null" ]]

do

curl -u "$user:$password" "https://mylink.dot" \
    | tee >(jq -r '.dataSummaries[] | .dataId' >> my_data_file.txt ) \
    >(jq -r '.nextToken'>next_token_file.txt) > /dev/null

next_token=$(cat next_token_file.txt)

done

I tried this:

while  [[ "$next_token" != "null" ]]

do
index=$((index+1))

curl -u "$user:$password" "https://mylink.dot" | tee >(jq -r '.dataSummaries[] | .dataId' | read my_array[index] ) >(jq -r '.nextToken'>next_token_file.txt) > /dev/null

next_token=$(cat next_token_file.txt)

done

When I use |read var my variable is empty.

How in this case can I store json data (and next token) in variable, not file.


Solution

  • The second jq should read from the standard output of tee, then write to standard output. This entire pipeline is then put in another process substitution for read to read from.

    read next_token <( curl -u "$user:$password" "https://mylink.dot" |
                         tee >(jq -r '.dataSummaries[] | .dataId' >> my_data_file.txt ) |
                         jq -r '.nextToken')