How to make a self-extracting shell script from a TAR archive

  8 years ago

Self extracting shell script is a shell script which can be run from terminal, and as a result of that, it can extract the files/folders which was bound to it and do some additional work on the way.

As an example, say, you have a tar archive with source or binaries of a software. You can make a single script to install the software from the source or binaries in your linux system. After you have succesfull created the script, the TAR archive is no longer needed and the software can be installed by running the install script you just created. An example of this might be the installer of Netbeans IDE (it comes with a netbeans-* file).

You can put your whole software project in a TAR archive and make a single installer for that.

There are various methods to do this. Standard sh (Bourne shell) can be used too, but I am going to use bash in this tutorial and follow a way, basically from this journal.


1. Write a script:

Create a script named tartos with the following content:

BASEDIR=`dirname "${0}"`


[ "$payload" != "" ] || read -e -p "Enter the path of the tar archive: " payload
[ "$script" != "" ] || read -e -p "Enter the name/path of the script: " script

printf "#!/bin/bash
PAYLOAD_LINE=\`awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' \$0\`
tail -n+\$PAYLOAD_LINE \$0 | tar -xvz
#you can add custom installation command here

exit 0
__PAYLOAD_BELOW__\n" > "$tmp"

cat "$tmp" "$payload" > "$script" && rm "$tmp"
chmod +x "$script"

Mind the line that says: #you can add custom installation command here.

You can put installation commands after this line, like

sudo make install

or whatever it is.


2. Use:

1. Give the tartos script execution permission.

chmod +x path_to_tartos_script

2. Now open a terminal in the folder where you have created the tartos script or cd to that directory and run:

./tartos archive_path script_name

or simply run the tartos script (it will ask for information on the way)

the self-extracting script will be saved in the working directory as script_name

 Now run this script_name to see the result. If everything's ok, you can ship this script as an installer for your software.


3. Supporting multiple TARs:


The above method will only work for .tar.gz archives.

There are other compression types for TAR archives:  .tar.xz .tar.bz2 .tar.lz etc...

For that you need to modify the line:

tail -n+\$PAYLOAD_LINE \$0 | tar xzv

in the tartos script. The z option will be modified here.


For .tar    z will be omitted.

For .tar.bz2    z will be replaced with j.

You can learn more about this option in the man page of tar.

man tar  //to access man page of tar

tar --help command will give you some good information too.


4. Automation:

If you don't want to deal with those commands or supporting multiple TARs and want to automate these tasks, you can use my script or there are other tools like shar (package sharutils) or makeself. I haven't used sharutils or makeself though, so it's upto you, whatever you find in those tools.

jahid_0903014 8 years ago

Hammer459, v is for verbose, I like to see if things are going well on the way :D.

I don't like things doing silently and showing nothing, specially when you need to confirm that, it actually works..

Hammer459 8 years ago

Nice except for the common misuse of the "v" flag to tar. Why vomit all the filenames in the tar?? tar xj or tar xz is more user friendly.