Skip to content

Instantly share code, notes, and snippets.

@td-shi
Last active January 9, 2022 00:53
Show Gist options
  • Save td-shi/965bdc451c3bcf00c34d0dfc950c86f8 to your computer and use it in GitHub Desktop.
Save td-shi/965bdc451c3bcf00c34d0dfc950c86f8 to your computer and use it in GitHub Desktop.
This script is the self-extracting archive template. The MD5 of archive data is a69fb4d0185baa34f1a809de747a22f7.
#!/bin/bash --posix
# -*- coding:utf-8 -*-
# === Coding shell scripting Memo ==============================================
# === Initialize shell environment =============================================
#set -u # Just stop undefined values.
#set -e # Just stop error.
#set -x # Debug running command.
umask 0022
export LC_ALL=C
export LANG=C
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:${PATH+:}${PATH-}"
type command >/dev/null 2>&1 && type getconf >/dev/null 2>&1 &&
PATH="$(command -p getconf PATH):${PATH}"
export PATH
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ================
usage_and_exit(){
cat <<-"USAGE" 1>&2
# About
This script is the self-extracting archive. Archives are extracted the <script>
directory. If the autrun.sh is contained archive, this script ask to run it.
This script require the tar( and gzip, gunzip) and openssl( or md5sum) command.
Archives in this script are the self template and the autrun shell script.
If you want to exchange the archive data, replace the end data block of this file.
The data block should be base64 encoded (`tar zcvf <target> | base64`).
# Usage
## Command
<script name> options
## Options
- -h |--help |--version
+ This help.
- -x
+ Permitting to extract. If the option -x is omitted, the program exits with abnormality 1.
- -c <sum>
+ Checking self-sum and <sum> and Skiping to confirm.
- -l
+ Show file list in archive.
# Version
2022-01-06 0.01
# LICENSE
[CC0(Public domain)](https://creativecommons.org/publicdomain/zero/1.0/legalcode)
# Author
2022 td-shi
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit "$1"
}
# === Initialize parameters ====================================================
SELF_DIR="$(cd "$(dirname "${0}")" && pwd)"
SELF_NAME="$(printf "%s" "${0##*/}" | sed "s/\.sh//")"
SELF_FULL="${SELF_DIR}/${0##*/}"
SUM_DGST="md5"
PERMIT_EXTRACT="0"
CHECK_SUM=""
CORRECT_SUM="0"
ONLY_LIST="0"
# === Confirm that the required commands exist =================================
if type tar >/dev/null 2>&1; then
:
else
error_exit 1 'Require the tar command.'
fi
if type openssl >/dev/null 2>&1; then
ckdgst() {
cat - |\
openssl dgst -"${SUM_DGST}" |\
sed "s/(.*)=[[:blank:]]*\([0-9a-fA-F]*\)/\1/"
}
elif type md5sum >/dev/null 2>&1; then
SUM_DGST="md5"
ckdgst() {
cat - |\
md5sum |\
cut -d " " -f 1
}
else
error_exit 1 'Require the openssl command.'
fi
# === Print usage and exit if one of the help options is set ===================
case "$# ${1:-}" in
'1 -h'|'1 --help'|'1 --version') usage_and_exit;;
esac
# === Read options =============================================================
while :; do
case "${1:-}" in
-x)
PERMIT_EXTRACT="1"
shift
;;
-c)
CHECK_SUM=$(printf '%s' "${2:-}" | tr -d '\n')
shift 2
;;
-l)
ONLY_LIST="1"
shift
;;
--archive-template)
sed '/^cat <<-\"==END\"/ ,$d' "${0}" |\
sed '$acat <<-\"==END\"' |\
sed '$a==END'
exit 0
;;
--|-)
break
;;
--*|-*)
error_exit 1 'Invalid option'
;;
*)
break
;;
esac
done
# === Require parameters check =================================================
# === Last parameter ===========================================================
# === Define funcitons =========================================================
confirm() {
while true; do
printf "%s [y/N]\n" "${1:-Do you wish to continue?}" 1>&2
read -r yn
case "${yn}" in
[yY]| [yY]es)
return 0
;;
*)
return 1
;;
esac
done
}
decord() {
SED_LF=$(printf '\\\n_')
SED_LF=${SED_LF%_}
cat - |\
tr -d "=" |\
sed "s/[[:blank:]]//g" |\
tr "01" "<>" |\
sed "s/./&${SED_LF}/g" |\
sed "/^$/d" |\
paste -d "" - - - - |\
sed "s:/:111111:g" |\
sed "s:+:111110:g" |\
sed "s:9:111101:g" |\
sed "s:8:111100:g" |\
sed "s:7:111011:g" |\
sed "s:6:111010:g" |\
sed "s:5:111001:g" |\
sed "s:4:111000:g" |\
sed "s:3:110111:g" |\
sed "s:2:110110:g" |\
sed "s:>:110101:g" |\
sed "s:<:110100:g" |\
sed "s:z:110011:g" |\
sed "s:y:110010:g" |\
sed "s:x:110001:g" |\
sed "s:w:110000:g" |\
sed "s:v:101111:g" |\
sed "s:u:101110:g" |\
sed "s:t:101101:g" |\
sed "s:s:101100:g" |\
sed "s:r:101011:g" |\
sed "s:q:101010:g" |\
sed "s:p:101001:g" |\
sed "s:o:101000:g" |\
sed "s:n:100111:g" |\
sed "s:m:100110:g" |\
sed "s:l:100101:g" |\
sed "s:k:100100:g" |\
sed "s:j:100011:g" |\
sed "s:i:100010:g" |\
sed "s:h:100001:g" |\
sed "s:g:100000:g" |\
sed "s:f:011111:g" |\
sed "s:e:011110:g" |\
sed "s:d:011101:g" |\
sed "s:c:011100:g" |\
sed "s:b:011011:g" |\
sed "s:a:011010:g" |\
sed "s:Z:011001:g" |\
sed "s:Y:011000:g" |\
sed "s:X:010111:g" |\
sed "s:W:010110:g" |\
sed "s:V:010101:g" |\
sed "s:U:010100:g" |\
sed "s:T:010011:g" |\
sed "s:S:010010:g" |\
sed "s:R:010001:g" |\
sed "s:Q:010000:g" |\
sed "s:P:001111:g" |\
sed "s:O:001110:g" |\
sed "s:N:001101:g" |\
sed "s:M:001100:g" |\
sed "s:L:001011:g" |\
sed "s:K:001010:g" |\
sed "s:J:001001:g" |\
sed "s:I:001000:g" |\
sed "s:H:000111:g" |\
sed "s:G:000110:g" |\
sed "s:F:000101:g" |\
sed "s:E:000100:g" |\
sed "s:D:000011:g" |\
sed "s:C:000010:g" |\
sed "s:B:000001:g" |\
sed "s:A:000000:g" |\
sed "s/[01][01][01][01][01][01][01][01]/& /g" |\
sed "s/ [01][01]$//" |\
sed "s/ [01][01][01][01]$//" |\
sed "s/^[01][01][01][01][01][01]$//" |\
sed "s/\([01][01]\)\([01][01][01]\)\([01][01][01]\)/0\1 \2 \3 /g" |\
sed "s/111/7/g" |\
sed "s/110/6/g" |\
sed "s/101/5/g" |\
sed "s/100/4/g" |\
sed "s/011/3/g" |\
sed "s/010/2/g" |\
sed "s/001/1/g" |\
sed "s/000/0/g" |\
tr -d " " |\
paste -d "" - - - - - - - - |\
sed 's/.../\\\\&/g' |\
xargs -n 1 printf
}
slice() {
sed "1,/^exit/d" "${1}" |\
sed "/^cat/d" |\
sed "/^==END/d"
}
# === Main routine =============================================================
if [ "_1" == "_${ONLY_LIST}" ]; then
slice "${SELF_FULL}" |\
decord |\
tar ztf -
exit 0
fi
SUM=$(slice "${SELF_FULL}" | ckdgst)
if [ "_" != "_${CHECK_SUM}" ] && [ "_${SUM}" == "_${CHECK_SUM}" ]; then
CORRECT_SUM="1"
elif [ "_" == "_${CHECK_SUM}" ] && confirm "Is the value of ${SUM_DGST} ${SUM} correct?"; then
CORRECT_SUM="1"
else
CORRECT_SUM="0"
fi
if [ "_1" != "_${CORRECT_SUM}" ]; then
error_exit 1 "No correct checksum."
elif [ "_1" != "_${PERMIT_EXTRACT}" ]; then
error_exit 1 "Does not exist -x option (to permit extracting). More info is gave -h option."
else
:
fi
mkdir -p "${SELF_DIR}/${SELF_NAME}"
(
cd "${SELF_DIR}/${SELF_NAME}" || exit 1
slice "${SELF_FULL}" |\
decord |\
tar xzvf -
if [ -e "autorun.sh" ] && confirm "Do you want to run the autorun.sh script?" ; then
chmod +x "autorun.sh"
./autorun.sh
fi
)
# === End shell script =========================================================
exit 0
cat <<-"==END" 1>&2
H4sIAAAAAAAAA+0a+1vbRjI/66/Yyg7GBFmSIdAq2Pk4cBJaIByPNj1MXCGtbR225OgB5vW/3+ys
JEvGa6BJL99dNfmCpZ2nZmdmnwEddBU6Dn3TCpXA8p1RqIR0OBqYIa0F/RffAjSAtbVV9quvv9ay
v/xRX3uhr+prayuatr6y/kKra6/XV14Q7ZtofwSiIDR9Ql7YNKB+2BuYQTCT7jH8/yiUflDPHVc9
N4M+UZSRFzhjqUSUJYVYnu24PSMKu8qPrEGC9kajQbawnQR9OhgQHjHsfY8OPYZ/BsQCd1wndMyB
c0NjodS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
ZAl/pO99mv98mPjsr9OB9z9WRfc/tPXVup7e/1jT1l5o+urrlfXi/sd/A77z/Y/y7QYezJY2RmYY
Ut9t3hPDIEMztPpMqk0HFCYZeFIa9D0/pFBMz6/Zsf8VlKJaVsRTZAw8tycW8fKZVpyb1sUDGQ+F
zLNilgh145yCdezB7IIkLig5T/fcARjv+MCfGJhjns9tQqfNYjOUDRycOTWUcRfKNA3xrAYRy8le
JL7lWBvPYsVTIsDhIbRcIxvZSGrKeLnCIxeUjogVwagMwwSnYHwhuybiXVI/e0moNuNo92tvEn3N
RaJn3yMqrhH9Pa4RuaEkZeY5EOmiWysZqtP47PKMbDSffG8lc2tl5lWPH7/HVQ9WLf7/L3qUavkJ
d66+8WlwT8GbT1Ds0tjl7b6Ja4S4xH77uxgwsiswsp8c7rJrX79BzpFFOg7M4WgA/VWC2k147kLl
HTDDZt9OKMECbG+78+H4+KBRYaQVqTS52HDFxD6Nl5EiL6xuStNHp7AsY1TK+9axChl8nBacLmQS
u4RQKm4h8HojuoWg3NzcNKZOFVnTg4sDeonTzr09kCQgEN4/5zoA0D/BhMfuLjyifc6thr/zkf7E
yeDQ1H3g5h5MCInS+kIqn8t3n5W37MZS7ezVcvpUrrDNgduHaZl8vjJYVhQ2e8CSlTijlJbfb3mR
AEMcUtLB+qFVyYbjjqKwCWGEB9VV1sxdCg969WFOlGKnVxRlVqomaMBXHkqfiJ507oRIz3UpJ8JO
zdq0lNqkM3tiOzB037zJkidi5fKSnJHAKIgISqQVWCaUXm2sbbIJjzbWW1PznW9x/+Iv2DtN7mps
nhx/JIcn+8w3L4NafGUjHUXlqbnWX7ev870X5AUUUEABBRRQQAEFFFBAAQUUUEABBRRQQAEFFFBA
AQUUUEABfxL+A8fv1jIAUAAA
==END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment