Bash script that process input from pipeline or file redirection

November 15th, 2008 mysurface Posted in Bash, pipeline | Hits: 196930 | 8 Comments »

I remember I wrote a post regarding how to makes python processing string from the pipeline stream. This time, I find the needs to do it in bash script.

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 will do this:

./ "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:

 grep "hex" dummy.log | cut -d"]" -f2 | ./

or like this:

 ./ < dummy.log

How to write this


if [ -e $1 ] ;then read str; else str=$1;fi
len=`expr length $str`
for (( a=0; a<=$len; a=a+2 )); do echo -n ${str:a:2}" "; done
echo ""

The important line is the if statement. If there are no argument specified ( -e $1), I read from the stream, else i take it from $1 ( param 1). It is so simple isn't it?

Have fun!

8 Responses to “Bash script that process input from pipeline or file redirection”

  1. a<=$len inside the C-style for loop needs to be a<=len or a<=”$len”

  2. Aia: just a<=$len is working for me.

  3. is there a way to become a content writer for the site?

  4. 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=\"$*\"

  5. @Ryan, I always get myself confused with with bash syntax :S You are right, should be replace with -z, or rearrange like you suggested. Thanks Ryan.

  6. FYI, this can be accomplished with a single “sed” command:

    sed ‘s/\(..\)/\1 /g’ <<< "5F443D95FEA3D4787AEDC4"


    sed 's/\(..\)/\1 /g' < dummy.log

    (It says "replace pairs of characters with the same two characters followed by a space".)

  7. For what reason almost anything you’ve find out about E commerce is undoubtedly drastically wrong and what you must learn.

  8. With thanks for sharing your awesome site

Leave a Reply