Skip to content

Instantly share code, notes, and snippets.

@cwilper
Created October 10, 2011 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwilper/1274547 to your computer and use it in GitHub Desktop.
Save cwilper/1274547 to your computer and use it in GitHub Desktop.
Small Self-Extracting Archiver
#!/bin/sh
# Name : sself
# Created : 2011-10-09
# Author : cwilper
# Purpose : Creates a self-extracting archive (a shell script containing a
# compressed tar file) out of the content of a given directory. When
# executed, the archive will be expanded into /tmp/sself/ (which will
# be cleared first if it already exists), and if a "run" script exists
# in the root of the archive, it will be executed. Standard output and
# standard error will be output to /tmp/sself/run.log. This is similar
# to the more feature-rich makeself[1], but is designed to add as little
# overhead as possible to the archive.
# Usage : sself INPUT_DIR OUTPUT_FILE
# Example : sself /path/to/input-dir my-archive.sh
# Related : [1] https://github.com/megastep/makeself
if [[ ! "$2" ]]; then
echo "Usage: $0 INPUT_DIR OUTPUT_FILE"
exit 1
fi
echo "#!/bin/sh" > $2
echo "m=\$(grep -a -n '^F:$' \$0|cut -d ':' -f 1)" >> $2
echo "s=\$((m + 1))" >> $2
echo "d=/tmp/sself" >> $2
echo "rm -rf \$d" >> $2
echo "mkdir \$d" >> $2
echo "tail -n +\$s \$0 | tar -x -z -C \$d -f -" >> $2
echo "if [ -f \$d/run ]; then" >> $2
echo " pushd ." >> $2
echo " cd \$d" >> $2
echo " ./run > run.log 2>&1" >> $2
echo " popd" >> $2
echo "fi" >> $2
echo "exit 0" >> $2
echo "F:" >> $2
tar -c -C $1 . | gzip >> $2
chmod 755 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment