How to generate a bash script with an embeeded tar.gz (self-extract)

Consider that you need to perform a routine in a remote server, where you need to decompress a tar.tz and execute a list of commands on this data. One alternative is send the tar.gz file to the remote server throught a ftp or scp and then log in the remote server and run a shell script or run manually a list of commands. Recall Java JRE setup, they use script.bin that comes with an embeeded tar.gz, which is self-extracted in the beginning of script execution. To build the self-extraction script I follow a tutorial published by Stuart Wells, which consists in four steps:

1) Create/identify a tar.gz file that you wish to become self extracting.

2) Create the self extracting script. A sample script is shown below:

> cat extract.sh
#!/bin/bash
echo "Extracting file into `pwd`"
# searches for the line number where finish the script and start the tar.gz
SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`
#remember our file name
THIS=`pwd`/$0
# take the tarfile and pipe it into tar
tail -n +$SKIP $THIS | tar -xz
# Any script here will happen after the tar file extract.
echo "Finished"
exit 0
# NOTE: Don't place any newline characters after the last line below.
__TARFILE_FOLLOWS__

3) Concatenate The script and the tar file together.

> cat extract.sh example.tar.gz > example.sh
> chmod +x example.sh

4) Now test in another directory.

> cp example.sh /tmp
> cd /tmp
> ./example.sh

See ya!

5 Responses to How to generate a bash script with an embeeded tar.gz (self-extract)

  1. Pingback: Breves « Ubuntu Life

  2. inukaze says:

    Me gustaria primero comprimir los videos del Quake 2 , y luego generar nuevamente , el archivo de autoextraccion “videos.run” , supongo que funciona de esta manera , pero como genero en un archivo “.run” ???

    Ya que la utilidad que disponia , me pedia que tubiera listo un script para crear el archivo “.run” , me refiero al Quake 2 para Linux que incluye un Instalador Grafico , y las expansiones. y mejoras graficas. tambien como los videos.

    Alguien me puede ayudar con esto ???

  3. emanuelvianna says:

    Hi inukaze,
    Let me see if I understood your problem, you want to be able to decompress a pack with several files (Quake installer for Linux, extensions and videos) running a script (./script.run) instead of manually decompress it (tar -xzf pack.tgz), right? Or, will you execute other commands using the decompressed pack files? Recall that this tutorial only applies to embed one compressed file, so you may generate a single compressed file with all your data. I suggest you (for testing) to compress a smaller directory (tar -czf directory.tgz directory) and append it to a script (cat directory.tgz >> script.run) containing the code above. Make script executable (chmod +x script.run) and run it. If you need to run other commands after decompress the directory (such enter in the directory, configure and execute a Quake installer) you may put your commands after the line 10. If you have other doubts feel free to ask.
    Best regards,
    Emanuel

  4. gjyang says:

    #remember our file name
    THIS_PATH=${0%/*}
    THIS_NAME=${0##*/}
    THIS=${THIS_PATH}/${THIS_NAME}

  5. Ehteshaam says:

    Thanks Emanuel.
    It is just fits my need.

    regards,
    Ehteshaam

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: