vendredi 9 novembre 2018

Suppress Errors in readarray Command in Bash

I'm parsing AWS policy documents and I'm trying to send the errors in this command to /dev/null so that the user doesn't see them.

This is my code:

readarray -t aws_policy_effects < <( if aws iam get-policy-version --policy-arn "$aws_policy_arn" --version-id "$aws_policy_version_id" --profile="$aws_key" 2> /dev/null | jq -r '.PolicyVersion.Document.Statement[].Effect'
          then
            true
          else
            aws iam get-policy-version --policy-arn "$aws_policy_arn" --version-id "$aws_policy_version_id" --profile="$aws_key" | jq -r '.PolicyVersion.Document.Statement.Effect'
          fi)

I'm using an 'if' statement so that the right jq query gets used based on the aws policy that we're reading.

I will get this error:

jq: error (at <stdin>:22): Cannot index string with string "Effect"

Because this command always run as the first condition of the if (if there's no list the statement of the AWS policy document):

++ jq -r '.PolicyVersion.Document.Statement[].Effect'
++ aws iam get-policy-version --policy-arn arn:aws:iam::123456789101:policy/IP_RESTRICTION --version-id v11 --profile=company-lab

Why isn't the error being buried by sending it to /dev/null? How can I get the error to not print out to the screen using this if statement?

Aucun commentaire:

Enregistrer un commentaire