Skip to content

Instantly share code, notes, and snippets.

@alexradzin
Last active September 18, 2024 15:36
Show Gist options
  • Save alexradzin/14ff2975632d18bb0c05 to your computer and use it in GitHub Desktop.
Save alexradzin/14ff2975632d18bb0c05 to your computer and use it in GitHub Desktop.
Script that creates self extracting executable script from tar.gz file.
#!/bin/sh
if [ $# -eq 0 ]; then
echo "This script creates self extractable executable"
echo Usage: $0 TAR.GZ [COMMAND]
exit;
fi
if [ $# -gt 0 ]; then
TAR_FILE=$1
fi
EXIT_COMMAND=exit
if [ $# -gt 1 ]; then
EXIT_COMMAND="exec $2"
fi
SELF_EXTRACTABLE="$TAR_FILE.self"
echo '#!/bin/sh' > $SELF_EXTRACTABLE
echo 'dd bs=1 skip=`head -3 $0 | wc -c` if=$0 | gunzip -c | tar -x' >> $SELF_EXTRACTABLE
echo "$EXIT_COMMAND" >> $SELF_EXTRACTABLE
cat $TAR_FILE >> $SELF_EXTRACTABLE
chmod a+x $SELF_EXTRACTABLE
@alexradzin
Copy link
Author

Script that creates self extractable executable from tar.gz file.
The result can be executed directly and extracts content of give tar.gz to current directory.
Script arguments: (mandatory) tar.gz file path and (optional) command that will be executed right after extracting the tar.gz content.

The name of resulting executable is as name of tar.gz with suffix ".self"

Usage:
Create self-extracted executable: ./selftar.sh my.tar.gz
Create self-extracted executable: ./selftar.sh my.tar.gz "folder/install.sh"

Both examples create file my.tar.gz.self

@alexradzin
Copy link
Author

@MartinVincent
Copy link

Thanks Alex, works very well.

@0wwafa
Copy link

0wwafa commented Sep 16, 2024

change this line:

echo 'dd bs=1 skip=`head -3 $0 | wc -c`  if=$0  | gunzip -c  | tar -x' >> $SELF_EXTRACTABLE

to:

echo 'dd status=none bs=`head -3 $0 | wc -c` skip=1 if=$0  | gunzip -c  | tar -x' >> $SELF_EXTRACTABLE

and it will be 10 times faster :D

@jbraseth
Copy link

change this line:

echo 'dd bs=1 skip=`head -3 $0 | wc -c`  if=$0  | gunzip -c  | tar -x' >> $SELF_EXTRACTABLE

to:

echo 'dd status=none bs=`head -3 $0 | wc -c` skip=1 if=$0  | gunzip -c  | tar -x' >> $SELF_EXTRACTABLE

and it will be 10 times faster :D

Could you explain why this makes it faster?

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