How to generate a bash script with an embeeded tar.gz (self-extract)
September 4, 2011 3 Comments
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!
Pingback: Breves « Ubuntu Life
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 ???
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