I created a hex string splitter script for my friend who needs to decode the raw data that will be written in hex string such as “5F443D95FEA3D4787AEDC4″. Every time, he split them byte by byte manually into this form (“5F 44 3D 95 FE A3 D4 78 7A ED C4″) for better reading. He was complain that the manual process was always bothering him. I told him that can be done by writting a simple bash script.

My hexsplit.sh will do this:

./hexsplit.sh "5F443D95FEA3D4787AEDC4"

But what if he grep this hex string from other file? Or what if the hex string are stored in a file? I want my bash script to be able to process hex string from pipeline stream as well as file redirection too, like this:

I learned a few new things about bash today thanks to this page. I had never seen
\"for (( expr; expr; expr ))\" syntax before. Was amazed when it worked. For anyone else that\’s new to it, in bash, do `help -m for` to get the simple details.

I was also not aware that you could do character-chopping inside of variables with the ${MYVAR:0:3} syntax… that is crazy. I can\’t believe I\’ve never seen that before. Very cool. THANKS!

One problem with your script though. Your if statement is wrong. `test -e $1` is going to run a file test on whatever $1 is, to see if it is a path to a file existing on the system. -e does not mean \"empty\". What you\’d want in it\’s place is \"-z\"… though, personally I would reorder the logic and perhaps enhance it at the same time to allow for multiple args being converted:
if [ $# -eq 0 ]; then
read str
else str=\"$*\"
fi