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,
Shell Scripting – Creating a Binary file
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.
Contact Us