1
  RECORDS=$(aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID \
   | $JQ -r '.ResourceRecordSets[] | select (.Name == "ap.com.") | .Name')

The Output value from $RECORDS prints in the format shown below in separated lines.

>> echo "recordType: $RECORDS"
recordType: ap.com.
ap1.com.
ap2.com.

How to print output in the format as (in inverted commas separating by comma)

>> echo "recordType: $RECORDS"
recordType: "ap.com, ap1.com, ap2.com"
0

4 Answers 4

4

I would read the jq output into an array, and use bash's own ability to join things:

# redirect from a process substitution into mapfile 
mapfile -t records < <(aws ...| jq ...)

printf 'recordType: "%s"\n' "$(IFS=,; echo "${records[*]}")"

That joins with just a comma, not comma+space:

recordType: "ap.com.,ap1.com.,ap2.com."

Get out of the habit of using ALLCAPS variable names, leave those as reserved by the shell. One day you'll write PATH=something and then wonder why your script is broken.

Sign up to request clarification or add additional context in comments.

4 Comments

I'd like to +10 just for the recommendation to stop using ALLCAPS. I think all shell related answers should contain that advice until that practice is purged from the world
Is there an 'official' source of the recommendation of not using ALLCAPS ? We see many examples of using it, like ORACLE_HOME, JAVA_HOME, or DOCKER_HOST, which is much more recent than the first two.
I suppose I should say "don't use allcaps local or script variables". Conventionally, environment variables are allcaps. And I would not expect there to be an official source: it's not up to the GNU bash maintainers to tell programmers not to make mistakes.
That makes much more sense. But if we look at configure script of bash source, we can see loads of ALLCAPS local or script variables, like PACKAGE_NAME, PACKAGE_VERSION, ... How can we explain it ?
0

Try this line :

echo "recordType: $RECORDS"|sed -e "s/\.$/\", /"|sed -e "s/ap/\"ap/"|tr -d '\r\n'| rev | cut -c 3- | rev

Not probably the most short and efficient but it works ;) (and it could be easily customize)

Comments

0

You can use:

echo recordType:\"$RECORDS\"|sed 's/ /,/g'|sed 's/:/: /'

Where the first sed to replace the commas, the second one to add the one space after the colon.

Comments

0

You can just change slightly your jq command :

records=$(aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID \
   | $JQ -r '[.ResourceRecordSets[] | select (.Name == "ap.com.") | .Name]|join(", ")')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.