The solution is simple to explain: The self-extractable file are joint from an extractor shell script and an tar archive, which contains the actual contents to be extracted and installed into the system. The extractor script is a text file while the archive file is binary. They are joint together using
cat to make the self-extractable archive. Thus, the self-extractable archive file should have the following parts:
- Script part: The extractor shell script, extract.sh:
#!/bin/bash
#
# the extractor script
#
# create a temporary directory
TMP_DIR=$(mktemp -d InstXXXXXX)
# extract the archive
ARCHIVE=`awk '/^__ARCHIVE_BEGIN__/ {print NR + 1; exit 0; }' $0`
tail -n+$ARCHIVE $0 | tar xzvf -C $TMP_DIR
## Alternatively you can use sed
#sed -e '1,/^__ARCHIVE_BEGIN__$/d' "$0" | tar xzvf -C $TMP_DIR
CUR_DIR=$(pwd)
# execute install.sh
cd $TMP_DIR; $TMP_DIR/install.sh
cd $CUR_DIR
rm -rf $TMP_DIR
exit 0
__ARCHIVE_BEGIN__
- Archive Part: The tar.gz archive contains the contents to install. Inside the archive, there should be an installer script, which is executed when extractor script has extracted the archive into a temporary directory. Therefore, the archive should contains below:
Also, we use a builder script:
build.sh to create the self-extractable archive file.
- To start, we need to prepare thhe directory structure like below:
./archive
./archive/bin
./archive/doc
./archive/install.sh
./archive/lib
./build.sh
./extract.sh
The archive directory is the directory contains the install.sh and the actual content files to install. The extract.sh file is extractor script.
- The build.sh script:
#!/bin/bash
#
# The builder script
#
cd archive
tar pczvf ../arc.tar.gz ./*
cd ..
cat extract.sh arc.tar.gz > install.bin
rm arc.tar.gz
chmod +x install.bin
- The final self-extractable installer: install.bin
See Also
No comments:
Post a Comment