Skip to content

Instantly share code, notes, and snippets.

@mrchristine
Created December 5, 2019 15:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrchristine/072bf0411375063e15e9c7032e93a450 to your computer and use it in GitHub Desktop.
Save mrchristine/072bf0411375063e15e9c7032e93a450 to your computer and use it in GitHub Desktop.
Decode and pretty print an encoded error message from AWS
#!/bin/bash
# grab decoded error message
error=`aws sts decode-authorization-message --encoded-message $@ | jq .DecodedMessage`
# trim the start and end double quotes
json_err=${error:1: -1}
# remove escaped quoted strings and pretty print with jq
echo $json_err | sed 's|\\"|"|g' | jq .
@billchen8888
Copy link

When I run it -, I get error message like the following:

line 9: -1: substituting expression < 0

@mrchristine
Copy link
Author

How are you calling this script?

You should be passing the encoded string into the execution of this script after making it an executable.
e.g. ./decode_aws_error.sh some_encoded_string_here

@robpearce-flux
Copy link

robpearce-flux commented Feb 21, 2022

you need to quote line 6.
json_err="${error:1:-1}"

@mkisono
Copy link

mkisono commented Aug 19, 2022

I worked for me after I changed the code like so.
json_err=${error:1:${#error}-1}

@juner417
Copy link

Thanks for you guys works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment