0

Been having some issues with variables and sed. XMLStarlet removes the newlines from the XML which are required for openssl to read and replaces them with spaces

When executing

xmlstarlet sel -t -v '//DecryptionKey' "file.xml" | sed $"s/ /\\\r\\n/g"

the key gets formatted properly with \r\n instead of spaces

however running the exact same piece of code in the bash script nothing gets changed, same exact space containing string

XML_DecryptionKey=$(xmlstarlet sel -t -v '//DecryptionKey' "$1" | sed $"s/ /\\\r\\n/g")

I have no clue what to do, I tried it with perl, tr, awk but it just wont replace in the script.

Edit:

XML File

<?xml version="1.0"?>
<KDM>
<Metadata>
<ProjectName>U2FsdGVkX19cW2e8E2ZrzV3q18NGPmGJWCWTttOd+S4=</ProjectName>
<Encrypted>true</Encrypted>
<CustomerID>U2FsdGVkX1/SF8OjyGWfCxB3DMm6PxR/L4o6YhvcHBM=</CustomerID>
<Expires>U2FsdGVkX19HzhPXAC8tnr6P5C8Rl+4TKhMdviwrQ77v0GVhrmXDE95dq2A9PUQc
uIraONz1A4CVzlp7BbECmw==</Expires>
</Metadata>
<DecryptionKey>U2FsdGVkX19V/n561XAGZWpPSRsQ80iAtw5liYzYMfhkJ6m2XfMaOj59AmP4+H1x
4Y6n3I2WrfNEjDqoNsqkapoi+DCMDQQhwfVkj3LiuFUOIi+dzqz84xnNDzobsMb0
...
znVSKgDdLFd0EE8d3u83IlkLd+AloxfjZBjxcaLg9qOdWXFYWBr1S5/mObAJiGoO
x1K1W19T9TR48rDOLZ7zgw==</DecryptionKey>
</KDM>

Image:

XML

XMLstarlet

SelectJB
  • 25
  • 6
  • 1
    What is the command you're using to run the script? – that other guy Jan 13 '20 at 22:59
  • ./xmldecrypt.sh file.xml – SelectJB Jan 13 '20 at 23:03
  • 1
    Do you have a shebang in the file? What is it? – that other guy Jan 13 '20 at 23:10
  • The shebang is `#!/bin/bash` – SelectJB Jan 13 '20 at 23:11
  • 1
    I'm unable to reproduce this. Here's what I did: 1. Create a `file.xml` containing ` Foo Bar Baz `. 2. Run your shell command, the output is three lines `Foo\r`, `Bar`, `Baz` without trailing linefeed. 3. Put your script command in a file `xmldecrypt.sh` with `#!/bin/bash` before it and `printf '%s' "$XML_DecryptionKey"` after. 4. `chmod +x xmldecrypt.sh`. 5. Run `./xmldecrypt.sh file.xml`. The output is again three lines with `Foo\r`, `Bar`, `Baz` without trailing linefeed, exactly as before. How do I reproduce this? – that other guy Jan 13 '20 at 23:23
  • Just posted the XML file (cropped decryption key) to the original file, it also seems that xmlstarlet converts the new line to a space and I dont know how to prevent it (see image) – SelectJB Jan 13 '20 at 23:28
  • Are you seeing this from your original command? You're not doing `echo $XML_DecryptionKey` right? – that other guy Jan 13 '20 at 23:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205893/discussion-between-selectjb-and-that-other-guy). – SelectJB Jan 13 '20 at 23:36

0 Answers0