Skip to content

Instantly share code, notes, and snippets.

@gregjhogan
Last active November 6, 2023 01:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gregjhogan/bfcffe88ac9d6865efc5 to your computer and use it in GitHub Desktop.
Save gregjhogan/bfcffe88ac9d6865efc5 to your computer and use it in GitHub Desktop.
self-extracting shell script
# create files in an otherwise empty directory
mkdir files
cd files
touch setup.sh # entry point after extraction
touch file.txt # supporting data used by setup.sh
# create archive
tar -pczf ../archive.tar.gz *
cd ..
# create self-extracting script
cat <<EOF > install.sh
#!/bin/sh
cd \$(dirname \$0) && mkdir -p install && tail -n+4 \$0 | base64 -d | tar -pzxf - -C ./install && cd install && ./setup.sh >setup.log 2>&1
exit \$?
EOF
# append archive
base64 -w 0 archive.tar.gz >> install.sh
chmod ug+x install.sh
# test
mkdir test
cp install.sh test/
cd test
./install.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment