views:

1967

answers:

4

Some software (for ex. the NetBeans IDE) ship the Linux installers in .sh files. Curious about how exactly they 'package' a whole IDE into a 'shell script', I opened the file in an editor. I saw some plain text shell scripting code and then some random gibberish, which I reckon is 'binary' or non-plain text.

I am wondering how they mix plain shell scripts and then probably call the 'non-readable' stuff, which would be the binaries.

Any insight on this?

+23  A: 

Basically, it's a shell script prepended to a compressed archive of some sort, such as a tar archive. You use the tail or sed command on yourself (the $0 variable in Bourne shell) to strip off the shell script at the front and pass the rest to your unarchiver.

For example, create the following script as self-extracting:

#!/bin/sh -e
sed -e '1,/^exit$/d' "$0" | tar xzf - && ./project/Setup
exit

Then:

mkdir project
echo "#!/bin/sh" > project/Setup
echo "echo This is the setup script!" >> project/Setup
chmod +x project/Setup
tar czf - project >> self-extracting

Now, if you get rid of your old project directory, you can run self-extracting and it will extract that tar file and run the setup script.

Curt Sampson
How do the shell script interact with the archive contents?( which is probably is some other language- assembly/binary/bytecodes)
Amit
In many actual cases, the binary portion will be base64 encoded or some other such encoding so that the entire file contains only printable characters.
TokenMacGuy
Amit: once you've extracted the archive, it's just files on the disk, and you can do whatever any shell script can do with files on the disk. My example runs a setup script that was extracted from the archive.
Curt Sampson
Yes, Curt. Thank You very much, It was a very nice and simple explanation.
Amit
+13  A: 

You might want to check out makeself.sh

From the authors' notes.

makeself.sh is a small shell script that generates a self-extractable tar.gz archive from a directory. The resulting file appears as a shell script (many of those have a .run suffix), and can be launched as is. The archive will then uncompress itself to a temporary directory and an optional arbitrary command will be executed (for example an installation script).

Makeself archives also include checksums for integrity self-validation (CRC and/or MD5 checksums).

The makeself.sh script itself is used only to create the archives from a directory of files. The resultant archive is actually a compressed (using gzip, bzip2, or compress) TAR archive, with a small shell script stub at the beginning. This small stub performs all the steps of extracting the files, running the embedded command, and removing the temporary files when it's all over. All what the user has to do to install the software contained in such an archive is to "run" the archive [that is execute the script]

I am trying to keep the code of this script as portable as possible, i.e it's not relying on any bash-specific features and only calls commands that are installed on any functioning UNIX-compatible system. This script as well as the archives it generates should run on any Unix flavor, with any compatible Bourne shell, provided of course that the compression programs are available.

Finally, the makeself package itself comes as a self-extracting script called makeself.run.

nik
+6  A: 

Add a Binary Payload to your Shell Scripts

seb
+4  A: 

GNU sharutils:

http://www.gnu.org/software/sharutils/

is a toolset for creating shell archives, and provides some additional features that may be helpful (such as checksums to ensuring that the payload is not damaged in transit).

Protecting against malicious modifications is not really feasible when the final product has to be interpretable by the shell - anyone who understood the generation technique could modify the checksum as well.

James F