自解压脚本安装包

免责声明:发的时候发现还有挺明显而且害有点严重的bug(但反正一般情况差不多够用了就以后再努力罢(ry

先上代码.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
TARGET=/opt/
CHKSUM=<checksum_of_your_payload>

mktemp=/usr/bin/mktemp
echo=/usr/bin/echo
sed=/usr/bin/sed
awk=/usr/bin/mawk
dd=/usr/bin/dd
rm=/usr/bin/rm
md5=/usr/bin/md5sum
# or whichever checksum is preferred
tar=/usr/bin/bsdtar

TMP=$(${mktemp} -d)
PAYLOAD="${TMP}/sysupgrade.bin"

if [ $? -ne 0 ] ; then
    $echo "Failed to create temporary directory."
    exit 1
fi

# Extract the payload
$sed -e '1,/^exit$/d' "$0" > ${PAYLOAD}

# Verify payload integrity. Change to preferrence accordingly
if [ "$($md5 ${PAYLOAD} | $awk '{print $1}')" != "${CHKSUM}" ] ; then
        $echo "Package integrity verification failed."
        exit 1
fi

# Pre-installation script here if necessary

# Expand the archive
$tar xf "${PAYLOAD}" -C "${TARGET}"

# Post-installation script here if necessary

$rm -rf "${TMP}"
$echo "Installation finished successfully."
exit

用法:

1
2
cat installer.sh "${PAYLOAD}" > package.sh
chmod +x package.sh

项目源自只想一键升级的甲方其实不是甚喵新鲜玩意,反正是想起了老黄的Linux驱动.sh(摊爪)适合批量部署软件的情况,或者自己搓好安装包留着日后偷懒(?)