Self-extracting script in sh shell

Viewed 9789

How would I go about making a self extracting archive that can be executed on sh?

The closest I have come to is:

extract_archive () {
    printf '<archive_contents>' | tar -C "$extract_dir" -xvf -
}

Where <archive_contents> contains a tarball with null characters, %, ' and \ characters escaped and enclosed between single quotes.

Is there any better way to do this so that no escaping is required?

(Please don't point me to shar, makeself etc. I want to write it from scratch.)

4 Answers

Yes, you can do it natively with xtar.

  1. Build xtar elf64 tar self-extractor header (you free to modify it to support elf32, pe and other executable formats), it is based on lightweight bsdtar untar and std elf lib.

    cc contrib/xtar.c -o ./xtar
    
  2. Copy xtar binary to yourTar.xtar

    cp ./xtar yourTar.xtar
    
  3. Append yourTar.tar archive to the end of yourTar.xtar

    cat yourTar.tar >> yourTar.xtar
    chmod +x yourTar.xtar
    
Related