Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Create Binary File from Shell Script

    How to Create Binary File from Shell Script

    By RahulJanuary 22, 20193 Mins Read

    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
    

    binary command shell
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Difference Between ZSH and BASH?

    What are the difference between ZSH and BASH?

    What is Secure Shell (SSH)?

    What is Secure Shell (SSH)?

    A Deep Dive into Set Options in Bash

    View 9 Comments

    9 Comments

    1. sunil on July 28, 2020 6:42 am

      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.

      Reply
    2. Laith on September 11, 2019 6:02 pm

      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.

      Reply
    3. B on August 10, 2019 4:14 am

      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?

      Reply
    4. Shivakumar on April 15, 2019 3:33 pm

      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 ?

      Reply
      • Vijay on June 25, 2019 6:34 am

        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

        Reply
        • Sunil on July 28, 2020 8:54 am

          So on remote machine GCC not required? Will that system admin able to regenerate original file?

          Reply
    5. Sudama on May 8, 2018 10:39 am

      Hello,

      The article was very helpful
      .
      Thanks

      Reply
    6. Dnyaneshwar on January 28, 2016 6:49 am

      Hi Rahul

      What about script.sh.x.c ? do we need to keep it for any further use ?

      Reply
      • jcoy0907 on January 21, 2019 3:56 pm

        No, that is just a workspace so the script can compile using c

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting and Getting the Default Timezone in Python
    • What is Media Access Control (MAC) Address?
    • What is Cross-Site Scripting (XSS)?
    • What is Content Security Policy (CSP)?
    • A User’s Guide to Understanding Redirection Operators in Bash
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.