Shell Scripting - Creating a Binary file
Last Updated :
07 Nov, 2022
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 script to convert binary to decimal Binary and decimal are two fundamental number systems used in computing. While humans typically interact with decimal numbers (0-9), computers internally store and process data using binary digits (0s and 1s). Converting between these systems is often necessary when working with computer programs or
5 min read
How to Create a Shell Script in linux Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 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 understandi
8 min read
Creating and Running bash and zsh Scripts Creating and running scripts can be a powerful tool for automating tasks and streamlining workflow. Bash and Zsh are two popular shells that can be used to create and run scripts on Linux and macOS systems. Bash, which stands for Bourne-Again Shell, is the default shell on most Linux distributions a
7 min read
Linux Shell Script to Backup Files and Directory Backing up our important files and directories is crucial to safeguarding our data. In this article, weâll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script weâll build allows us to specify which
5 min read
How to Run a Shell Script in Linux Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
Shell Script Examples For all the Linux distributions, the shell script is like a magic wand that automates the process, saves users time, and increases productivity. This shall scripting tutorial will introduce you to the 25 plus shall scripting examples. But before we move on to the topic of shell scripting examples, l
15+ min read
Difference Between a Script file and a Binary file In this tutorial, we will learn what a script file and a binary file are and what's the difference between a Script file and a Binary file. Binary File A binary file is a file in which the content of the file is in binary format, the file data is not human-readable. Binary files contain formatted in
4 min read
Bash Scripting - How to check If File Exists In this article, we will write a bash script to check if files exist or not. Syntax :test [expression][ expression ][[ expression ]] Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: - -f: It returns True if the file exists as a com
6 min read
Create integer variable by assigning binary value in Python Given a binary value and our task is to create integer variables and assign values in binary format. To assign value in binary format to a variable, we use the 0b prefix. It tells the compiler that the value (prefixed with 0b) is a binary value and assigns it to the variable.Input: Var = 0b1010Outpu
2 min read