Last Updated: February 17, 2021
·
9.378K
· glucero

creating a self-extracting-archive installer script

First, tar and gzip the file(s):

tar -cvf archive.tar app_name/ ; gzip -9 archive.tar

Next, create the installer script (here's a basic one):

#!/bin/bash
function die () { echo "Error!"; exit 1 }
cd ~/ || die

echo "Installing AppName to ~/app_name..."

The script will have to find the embedded archive. So, we'll find the line number with our "ARCHIVE:" marker:

archive=$(grep --text --line-number 'ARCHIVE:$' $0)

Then the script can tail the file (starting after ARCHIVE:) and pipe the tailed binary output to gzip, then tar. No temporary files are created.

tail -n +$((archive + 1)) $0 | gzip -vdc - | tar -xvf - > /dev/null || die

We have some post installation configuration in our sample application:

./app_name/bin/post_install_configuration.sh || die

echo "Installation complete!"
exit 0

Don't forget to write the archive marker at the end of the script

ARCHIVE:

Save the file as "installer.sh"

Now we can add the archive to the installer:

cat archive.tar.gz >> installer.sh

Make it executable:

chmod +x ./installer.sh

All done!

2 Responses
Add your response

This looks nice, but unfortunately this is not working for me (trying this on openSUSE 15.2).
grep and tail fail with './insttaller.sh: file not found'
I guess the script is not readable while it is executed.

over 1 year ago ·

Ok, I have found the 'cd ~/' does change the current dir and obviously the $0 parameter which is './installer.sh' is not valid anymore.
I used 'ME=which "$0" to make the path absolute before the cd command is executed.

over 1 year ago ·