-
-
Save alexradzin/14ff2975632d18bb0c05 to your computer and use it in GitHub Desktop.
#!/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 |
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
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?
actually, from my test, this is fine for small files, but when you try to self-extract 2GB file, it get VERY slow..
i did following to make it way faster (almost as fast as normally extracting using tar zxvf )
change
echo 'dd bs=1 skip=`head -3 $0 | wc -c` if=$0 | gunzip -c | tar -x' >> $SELF_EXTRACTABLE
TO
echo 'tail -c +$(($(head -3 $0 | wc -c | tr -d " ")+1)) "$0" | tar zxvf -' >> $SELF_EXTRACTABLE
The reason for this is because dd bs=1 is an extremely inefficient way to skip the first few bytes of the file. It reads one byte at a time, causing significant performance overhead.
i also added tr -d " "
because this script would not work properly on MacOS (works fine on linux)
The reason, is that wc -c reports "spaces" in its output on Mac.. so we need to clear them with tr command.
now script works on both MacOS and Linux
Thanks Alex, works very well.