While working with the Linux systems, we used many of commands on daily basis. Most of the commands are available in binary format in /bin, /sbin , /usr/bin, /usr/sbin, etc directories. As a system administrator or student we wrote many of shell script to do few task or automate them. This article will help you to create binary file of your shell script, so no one can see the source code of your script and we can use them as a command. To create binary file from a script we use SHC compiler written by Francisco Javier Rosales García.

Advertisement

Follow the below steps to do this.

Step 1 – Prerequsities

First of all, You need to install required packages for SHC compiler.

For Ubuntu, Debian and LinuxMint

sudo apt-get install libc6-dev 

For CentOS, RHEL & Fedora

sudo yum install glibc-devel

Step 2 – Download and Install SHC

Download the latest source code of SHC compiler from its official webpage or using below commands and extract on your system.

cd /usr/src
wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
sudo tar xzf shc-3.8.9.tgz

Now compile the SHC source code on your system and install it using following command.

cd shc-3.8.9
make
make install

Step 3 – Create Shell Script

Let’s create a shell script as per your requirement or skip this step if you already created. For this article we have created below sample script which add the integer values given on command line parameter and show the sum of them.

vim script.sh
#!/bin/bash

total=0

for i in $@; do
   if [ ! -z "${i##[0-9]*}" ]; then
	echo "Please enter numeric only"
	exit 1
   fi
   total=$(($total + $i))
done

if [ $total -eq 0 ]; then
    echo "Plesae execute script like: $0 10 20 30"
    exit 0
fi

echo $total

Step 4 – Create Binary of Script

At this stage we have installed SHC compiler and have a shell script named script.sh. Use the following command to create binary file of your script.

shc -T -f script.sh

The above command will create two files in current directory. One will be script.sh.x.c which is in C language format of your script. Second one will be script.sh.x which will be in binary format.

Step 5 – Test Binary Script:

If you try to open binary format of script, you will see that it is not in human readable format.

Now move this script under /usr/bin directory to use from anywhere in system. Also remove .sh.x from file name. So it will be available with simple name. Also set the execute permissions to everyone

mv script.sh.x /usr/bin/script
chmod +x /usr/bin/script

Now type command ‘script’ from anywhere in system. You will see the same results as your shell script does.

script 10 20 30 

60
Share.

9 Comments

  1. Hi

    Can anyone regenerate the original file?

    If not, any other way to secure it so that even distributing my compiled file will not create the original file.

  2. Hi,

    when I run she -v -r -T -f scrip.sh I get the followings under Ubuntu 18.x.x.x

    she shll=env
    shc Unkown shell (env): specify [i] [-x] [-l]
    she: success

    No files with .c or .x were generated. Any way you could help me.

  3. For some reason this is not working anymore. I get the binary file + c files after shc -f script.sh but after changing permissions and moving to the /usr/bin folder, it just halts at the terminal, no execution at all.

    Any suggestions?

  4. Hi – I followed the steps above and was even able to generate the .c.x files. I was even able to run the .x script on the machine where i generated it. But when i copy the file to a different machine I am not able to run it. Is it mandatory to have libc6-dev and shc running on the remote machine also ?

    • use command “shc -v -r -T -f script.sh”. So that generated .c.x file will run on another remote machine having same operating system

Leave A Reply

Exit mobile version