Skip to content

Instantly share code, notes, and snippets.

@christianscott
Last active July 4, 2021 13:17
Show Gist options
  • Save christianscott/5445ce1d118f88199e7962e7d3330224 to your computer and use it in GitHub Desktop.
Save christianscott/5445ce1d118f88199e7962e7d3330224 to your computer and use it in GitHub Desktop.
Executable zip file
set -e
rm -rf out
mkdir out
cat >"index.js" <<EOF
console.log("hello from nodejs!");
EOF
zip -q out/archive.zip index.js
rm index.js
# escape all the $s so they're not eval'd till later
cat >"header.sh" <<EOF
#!/bin/bash
tmp=\$(mktemp --directory)
unzip -qo \$0 -d \$tmp
node "\$tmp/index.js"
exit
EOF
# This creates a zip file that looks like this;
# <header>
# <archive>
cat header.sh out/archive.zip > out/archive.zip.bak
mv out/archive.zip.bak out/archive.zip
# the "A" option adjusts the offset, telling zip where the actual
# archive starts
zip -qA out/archive.zip
chmod +x out/archive.zip
# $ bash executable_zip_file_demo.sh
# $ ./out/archive.zip
# hello from nodejs!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment