here-string or jq seems to behave differently with bash version

28 Views Asked by At

I'm looping through a json file with the following code :

#!/bin/bash

DS_SRC_FILE="/mnt/c/Users/foo/tmp/jboss_simple.json"

USERNAMES=$(jq -r 'to_entries[]| select(.value.policy_name)?| [.key, .value.username, .value.policy_name, .value.atg_type ]| join(" ")' ${DS_SRC_FILE})
while read  ds current_username policy atg; do
  echo -e "##########\nProcessing $ds..."
  echo -e "username is ${current_username}"
  echo -e "policy is ${policy}"
  echo -e "atg is ${atg}"
  
done <<< ${USERNAMES}

With the json file here

When running on my laptop with WSL :

$ jq --version
jq-1.6
$ bash --version
GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)

I do have what's expected :

$ ./toto.sh 
##########
Processing DS1...
username is user_1
policy is policy_1
atg is CORE
##########
Processing DS3...
username is user_2
policy is policy_2
atg is SWA
##########
Processing DS4...
username is user_3
policy is policy_3
atg is SWB

But when trying it from a linux box, with other bash and jq versions :

$ jq --version
jq-1.5
$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

It fails, because there is no carriage return after the 1st end of line :

$ ./toto.sh
##########
Processing DS1...
username is user_1
policy is policy_1
atg is CORE DS3 user_2 policy_2 SWA DS4 user_3 policy_3 SWB DS5 user_5 policy_4 AGT DS6 user_6 policy_5 PUB DS8 user_7 policy_6 COREPV DS9 user_8 policy_7 SWAPV DS10 user_9 policy_8 SWBPV DS15 user_10 policy_9 SWA DS15_batch_only user_11 policy_10 SWA DS16 user_12 policy_11 SWB DS16_batch_only user_13 policy_12 SWB DS17 user_14 policy_13 PROCESS DS18 user_15 policy_14 PROCESS DS19 user_16 policy_15 EDIT DS19_batch_only user_17 policy_16 EDIT DS20 user_18 policy_17 EDIT DS22 user_19 policy_1 IMPORT

How can I adapt to that, to have the same code on both boxes ?

0

There are 0 best solutions below