Using shc to compile a shell script

Not being a programmer, I do write a descent shell script from time to time.  In the Microsoft world, there are all sorts of script compilers that will compile a batch, visual basic, or any number of other scripts into an executable.  A benefit of an executable could be the files portability or even an attempt to obfuscate the code.  Another reason, to see if it can be done.  I favor the later reason.  Not having done this in Linux before, the quest has begun.

After surfing the web for a bit, I came across shc, a generic script compiler. First, I setup my working “environment”.

Setup shc

mkdir -p /opt/app/shc
cd /opt/app/shc
yum install -y wget gcc
tar xzvf shc-3.8.9b.tgz
cd shc-3.8.9b
make test
make strings
make install

Create a binary executable

I created a simple test script called myip.  I usually use /bin/bash, in the newer operating systems it is symbolically linked to /bin/sh.  However, to make it more portable, this script is using /bin/sh.

echo '#!/bin/sh
echo $(ifconfig eth0|awk '/inet addr/ {split ($2,A,":"); print A[2]}')' > myip

Compile the code.

shc -r -T -f myip

The result will be the creation of two files, myip.x and myip.x.c.  The myip.x is the executable and the generated c code is myip.x.c.  To run the new code.