Skip to content

Instantly share code, notes, and snippets.

@jcward
Last active August 25, 2022 06:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jcward/375e592b402961c135f5642d7bce1d24 to your computer and use it in GitHub Desktop.
Save jcward/375e592b402961c135f5642d7bce1d24 to your computer and use it in GitHub Desktop.
Quick bash script to create self-extracting .sh files /w Base64 encoded contents (e.g. binary, zip, etc)
#!/bin/bash
#
# create_self_extracting_script.sh <INPUT_FILE> [<OUTPUT_SCRIPT>]
#
# Creates a self-extracting shell script containing INPUT_FILE as Base64-encoded
# bytes. When executed, the script writes INPUT_FILE and restores its permissions.
#
# (c) Jeff Ward, 2017
# MIT License
FILENAME=$1
SCRIPT=${2:-extract.sh}
if [[ $# -eq 0 ]] ; then
echo "Usage: $0 INPUT_FILE [OUTPUT_SCRIPT]"
exit 0
fi
if [ ! -f "$FILENAME" ]; then
echo "File not found: $FILENAME"
exit 1
fi
echo "Writing $SCRIPT"
# Capture current permissions
PERMS="644"
if [[ "$OSTYPE" == "linux-gnu" ]]; then
PERMS=`stat -c "%a" "$FILENAME"`
elif [[ "$OSTYPE" == "darwin"* ]]; then
PERMS=`stat -f "%A" "$FILENAME"`
fi
cat > "$SCRIPT" << EOF
FILENAME="$FILENAME"
# Extract the .zip file
LINES=\`awk '/^__BASE64_ARCHIVE__/ {print NR + 1; exit 0; }' "\$0"\`
tail -n+\$LINES "\$0" | base64 --decode > "\$FILENAME" || { echo "Failed to write \$FILENAME" ; exit 1; }
# Restore permissions
chmod $PERMS "\$FILENAME"
echo "Extracted ./\$FILENAME"
exit 0
__BASE64_ARCHIVE__
EOF
base64 "$FILENAME" >> "$SCRIPT"
chmod a+x "$SCRIPT"
@jcward
Copy link
Author

jcward commented Feb 8, 2017

TODO:

  • OSX support
  • optional GPG symmetric encryption

@tapestreamer
Copy link

Does this also give the possibility to extract the zip and rebuild the installer manually? Im wondering how the data can be extracted and you can modify the installer without having the original files present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment