Is it possible to write a bash script, which would contain a binary executable program inside?
I mean a script, which would contain a dump of an executable in a textual form, which will be dumped back to an executable by this script when it is executed?
I would love to know a solution, which will work out of the box without a need of installing additional packages. Is it possible?
Thanks!
Sure it is!
You can for example dump any binary into a file with:
There are many installation scripts which do things like that.
You can convert your binary to text, and then back to binary using uuencode/uudecode.
http://linux.die.net/man/1/uuencode
So you can store your binary data as text in your script and output it to a binary file.
uuencode binaryFile > output.txt
And then put that data into your script and when creating the binary file use uudecode.
So, if I got it right you want to include a binary in your script and execute it on script exit?
Here is a
binarymaker
script(This does not only create a script that extracts a binary, but merges any your script with any binary):You should run it like this
If you run
resultscript
then bothmyscript
andmybinary
are going to be executed. You can optionally add a command torm
the binary afterwards.Also, do not forget to exit at the end of your script because otherwise it will continue and try to parse binary data.
If you're working with another script and not a binary, then it can be executed from pipe like this:
But it is not going to work on real binaries. Also, it doesn't work if your bash version is under 4
I tried this out and it works. Hex was generated with xxd.
Don't reinvent the wheel like several other answers are suggesting, just use the venerable shar command which is precisely doing this by design.
Assuming the file you want to embed in your script is
binaryfile
, simply runand you are set. You have a shell script named
binaryfile.shar
which when executed will extractbinaryfile
.i never done something like this before ;) this will compile some c source, create a
b.bash
script containing the binary (and the original script for simple development)(a.bash)
invoke with (a.bash generates b.bash):
i don't know how to evade writing out the binary into a temporary file before execution...