TTK
TTK

Reputation: 223

convert hex characters in a file to ASCII using a shell script

I have a file values who contains a string of hex characters.

\x41\x42\x43\x44\x45

I can encode it to the binary values using for example printf:

printf $(cat values)----------- output ---------> ABCDE

This works perfectly on the terminal, but it doesn't work inside a script:

#!/bin/sh
printf $(cat values)

Result:

$ ./script.sh
\x41\x42\x43\x44\x45

I would like to know why it doesn't work and how to solve it.

Upvotes: 0

Views: 1406

Answers (2)

Thomas Baruchel
Thomas Baruchel

Reputation: 7517

It doesn't work because printf used in bash isn't the same as in sh. For a quick fix, simply replace #!/bin/sh by #!/bin/bash. As long as you don't care about full compliance with /bin/sh, it may be an easy solution.

However, if you really want to use the sh interpreter, then use something like:

/bin/echo -e "\x40"

Upvotes: 1

Padawan
Padawan

Reputation: 313

Have you tried using #!/bin/bash and not #!/bin/sh? I can't tell from your question, but it doesn't seem as if you need to use sh instead of bash.

Upvotes: 0

Related Questions