The Wayback Machine - https://web.archive.org/web/20240913211224/https://www.geeksforgeeks.org/shell-scripting-creating-a-binary-file/
Open In App

Shell Scripting – Creating a Binary file

Last Updated : 07 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or automate them. In this article, we shall see how to create a binary file from our own shell script so that no one can see the source code and it can also be used as a normal system command.

What are Binary files?

A binary file is a type of computer file that is used to store data in the form of bytes. It can be used to store any kind of data either formatted or unformatted data encoded within the binary format. It is used directly by the computer and non-human readable which is also called binaries. The header of the binary file defines the file type and other relevant information like the block and body sizes. The header might include a few human-readable characters, but a binary file as a whole requires specific software or hardware to read the file.

Binary files are commonly used when building applications/software’s. But, programmers do not work directly with the binary files. Instead, they build their applications in a high-level programming language such as C, C++, or Java. The languages use human-readable text files to define the logic. At some point in their development cycle, application logic is compiled and converted into object code which is then called an executable or binaries with extensions such as .bin or .exe.

The below steps would help to create our own binary file from the shell script,

Pre-requisites

To create a binary from the shell script, the user needs to install the SHC (Shell Script Compiler) which can be done in two following ways,

Installation of shc

Method 1: From the repository

It can be downloaded and installed from the repository using the below commands,

For Ubuntu/Debian :

sudo apt install shc

For RHEL/Centos/Fedora:

sudo yum install shc

Method 2: Download and Install SHC

SHC can also be installed by downloading the source RPM from the official webpage using the below commands,

cd /root/shc

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

tar xzf shc-3.8.9.tgz

 

Now let’s compile the SHC source code and install it using “make install” which will copy the shc binary to /usr/local/bin directory,

cd shc-3.8.9
make install

 

Thus shc is successfully installed in the system,

 

Example of Binary File:

Step 1: Create a shell script

We will write a simple shell script add.sh which takes a number as input from the user and display the sum value.

#!/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 "Please execute script like: $0 10 20 30"
    exit 0
fi

echo "The sum is $total"

 

Step 2: Create a Binary

Using SHC, we will create a binary from add.sh using the below command,

shc -f add.sh

The above command creates two files in the current directory where the command is run. One will be add.sh.x.c which is in C language format and the other one is add.sh.x which is the binary file.

 

Step 3: Test the binary

Thus we have successfully created the binary file from our add.sh script and it’s time to move it to /usr/bin directory for the binary to work like a standard system command. Also, set execute permission for the script.

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

Now running the binary anywhere in the system would give the same result as the shell script does,

 



Similar Reads

Shell Scripting - Difference between Korn Shell and Bash shell
Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year back as it is older than the BASH shell and it has fe
3 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying details and consequences. To prevent this we use
5 min read
Shell Scripting - Interactive and Non-Interactive Shell
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them
3 min read
Shell Scripting - Default Shell Variable Value
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them
3 min read
Shell Scripting - Shell Signals Values
Prerequisites: Processes, Bash Scripting, Shell Function Library Signals help the operating system to communicate with processes and vice-versa. Signals are also generated when processes don't function properly or try to access prohibited memory. Different signals are mapped to different numbers which are referred to as signal values. The Linux OS
6 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with th
7 min read
Shell Scripting - Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which will be discussed in the article). Overall knowing
6 min read
Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
In shell scripting and Linux, the echo command is used to display text on the terminal or console. When used with the $SHELL variable, which contains the path of the current user's shell program, the output of the echo command can be different depending on whether the variable is enclosed in single or double quotes. echo "$SHELL" will expand the $S
4 min read
How to check if a directory or a file exists in system or not using Shell Scripting?
Shell scripting is really a powerful and dynamic way to automate your tasks. To test if a directory or file already exists in the system or not we can use shell scripting for the same along with test command. To proceed with the test script lets first check the test manual. To open a manual use the man command as follows: man test Right now we will
2 min read
Shell Scripting - How do we can create a sample ~/.dialogrc file
The dialog command allows you to display a variety of questions or messages from a shell script using dialog boxes. The ~/.dialogrc file is a run-time configuration file for dialog commands. Basically, it is the default configuration file. Also, the ~/.dialogrc file allows us to customize various aspects of the dialog command. If you do not have th
3 min read
Bash Scripting - How to Run Bash Scripting in Terminal
In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we print or echo some text on screen with the help of a t
2 min read
Bash Scripting - Working of Bash Scripting
Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and ot
6 min read
Bash Scripting - Introduction to Bash and Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to a
10 min read
How to protect Linux shell file using noclobber in bash shell?
Most Linux shells(bash, csh, ksh, tcsh) have a built-in file protection mechanism to prevent files from being overwritten accidentally. In this article, we will see how to prevent it by setting noclobber option. Clobbering Redirecting standard output to a file that already exists would overwrite the existing file content which will result in data l
2 min read
Array Basics Shell Scripting | Set 2 (Using Loops)
It is recommended to go through Array Basics Shell Scripting | Set-1 Introduction Suppose you want to repeat a particular task so many times then it is a better to use loops. Mostly all languages provides the concept of loops. In Bourne Shell there are two types of loops i.e for loop and while loop. To Print the Static Array in Bash 1. By Using whi
3 min read
Create a password generator using shell scripting
Creating a strong password is often a time-consuming task and even after creating a good password by yourself it gets brute-forced by hackers. In this article, we will learn how to create a strong password which fulfills all requirements including symbols, capitals length etc using a simple shell script in Linux. How to create a password generator
1 min read
Leap Year Program with Bash Shell Scripting in Windows
Accurately the Earth has 365.24 days per year, to correct the approximation and keep track of the Solar Calendar the idea of leap year has been introduced to Gregorian Calendar. A leap year comes once every four years. In contrast, it has 366 days with regular years, having 365 days. Mathematical approach to check:Every year is a Leap year if it is
1 min read
String Manipulation in Shell Scripting
String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content to a variable and printing its content: In bash, '
4 min read
Shell Scripting - Test Constructs
Since 0 by UNIX tradition denotes "success," an if/then construct checks to see if the exit status of a list of commands is 0 and, if it is, runs one or more instructions. There is an exclusive command called [ (left bracket special character). It is a built-in synonym for test and is used for efficiency. This command uses the arguments as file tes
6 min read
Shell Scripting - Select Loop
The select loop is one of the categories of loops in bash programming. A select-loop in the shell can be stopped in two cases only if there is a break statement or a keyboard interrupt. The main objective of using a select loop is that it represents different data elements in the form of a numbered list to the user. The user can easily select one o
5 min read
Shell Scripting - Define #!/bin/bash
A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A shell takes commands from the user and translates
3 min read
Shell Scripting - Command Substitution
A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use the terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions given by the user. This process involves taking c
4 min read
Shell Scripting - Substitution
There are certain expressions that convey special meanings. In other words, they are not what they look like. A shell carries out substitution whenever it encounters such expressions. Hence, substitution is defined as a mechanism carried out by a shell in which it substitutes the value of an expression with its actual value. Escape sequences: An es
3 min read
Shell Scripting - True Command
A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix commands, we use a shell. The main job of a shell i
3 min read
Shell Scripting - Variable Substitution
A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use a terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions given by the user. This process involves taking com
3 min read
Shell Scripting - Rules for Naming Variable Name
Variables are quite important in any script or program, so we need to understand what is the convention to name these variables. There are certain rules and standards to keep in mind while giving names to the variables in Shell scripting. In this article, we will discuss and list down all the rules and conventions to declare a variable name in Shel
4 min read
Shell Scripting - How to view Processes?
In this article, let's explore how we can view current processes running on a Linux Server. Linux machine gives users the option of multitasking hence naturally there will be multiple processes running on a session and also these processes are important fundamental components of Linux OS, Hence System Administrators may need to be aware of all proc
4 min read
Shell Scripting - Dialog Boxes
In this article, we will create a shell script that generates a Dialog Box with GUI generating a message to the user. What is a Dialog Box? A Dialog Box is a temporary window an application runs to convey important information to the users. These dialog boxes can be used to display warnings, errors, and also any additional inputs needed by the syst
7 min read
Shell Scripting - Readonly Command
In this tutorial, we will learn about the ‘readonly’ command. Using the readonly command, you can assign a particular value to a variable that can neither be changed nor modified by subsequent assignments nor be unset. Syntax:readonly name[=value] readonly [-aAf] [name[=value] Most used options: -p : It prints all the defined readonly variables-f :
3 min read
Shell Scripting - Test Command
A test command is a command that is used to test the validity of a command. It checks whether the command/expression is true or false. It is used to check the type of file and the permissions related to a file. Test command returns 0 as a successful exit status if the command/expression is true, and returns 1 if the command/expression is false. Syn
8 min read