Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a Executable Binary in a shell script

Tags:

java

c

shell

binary

First, I already googled but only found examples where a compressed file (say a .tar.gz) is embedded into a shell script.

Basically if I have a C program (hello.c) that prints a string, say Hello World!.

I compile it to get an executable binary

gcc hello.c -o hello

Now I have a shell script testEmbed.sh

What I am asking is if it is possible to embed the binary (hello) inside the shell script so that when I run

./testEmbed.sh

it executes the binary to print Hello World!.

Clarification: One alternative is that I compress the executable into an archive and then extract it when the script runs. What I am asking is if it is possible to run the program without that.

Up until now, I was trying the method here. But it does not work for me. I guess the author was using some other distribution on another architecture. So, basically this did not work for me. :P

Also, if the workflow for a C program differs from a Java jar, I would like to know that too!

like image 944
Ankit Avatar asked May 08 '12 02:05

Ankit


People also ask

What is $? == 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.

What is $() in shell script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.


2 Answers

The portable way to do this is with the printf command and octal escapes:

printf '\001\002\003'

to print bytes 1, 2, and 3. Since you probably don't want to write that all by hand, the od -b command can be used to generate an octal dump of the file, then you can use a sed script to strip off the junk and put the right backslashes in place.

like image 84
R.. GitHub STOP HELPING ICE Avatar answered Nov 03 '22 09:11

R.. GitHub STOP HELPING ICE


I think makeself is what you're describing.

like image 43
Diego Schulz Avatar answered Nov 03 '22 08:11

Diego Schulz