Recently, I got a block of standard od -c output and needed that in binary form. Here’s what I came up with:
!/bin/bash
while read -r p; do
IFS=' ' read -r -a array <<< "$p"
for index in "${!array[@]}"
do
if [ $index -ne 0 ]; then
CH=${array[$index]}
if [ ${#CH} -eq 3 ]; then
echo -en "\0${array[$index]}"
elif [ ${#CH} -eq 2 ]; then
echo -en "${array[$index]}"
elif [ ${#CH} == '|' ]; then
echo -en '|'
else
echo -en "${array[$index]}"
fi
fi
done
done
It works pretty well unless there are blanks in the input stream which cannot (yet) be detected. Fortunately, in my case there weren’t any.
Convert od output to binary