基本思路是创建一个文件,前半部分是一个Shell脚本,后半部分则是tar建立的压缩文件。Shell脚本调用tar解压文件的后半部分,再调用解压后的安装文件。本文给出的方法比较灵活,对安装脚本的行数没有要求。
先建立一个脚本文件,内容如下,存为inst_script.sh。注意将程序中间./installer一行修改为你的程序的安装程序地址。
注意:
文件末尾不要添加更多的空行。
<span class="co0" style="margin: 0px; color: rgb(102, 102, 102); font-style: italic;">#!/bin/bash</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">echo</span> <span class="st0" style="margin: 0px; color: rgb(255, 0, 0);">""</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">echo</span> <span class="st0" style="margin: 0px; color: rgb(255, 0, 0);">"Self Extracting Installer"</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">echo</span> <span class="st0" style="margin: 0px; color: rgb(255, 0, 0);">""</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">export</span> <span class="re2" style="margin: 0px; color: rgb(0, 120, 0);">TMPDIR</span>=<span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">`</span><span class="kw2" style="margin: 0px; color: rgb(194, 12, 185); font-weight: bold;">mktemp</span> <span class="re5" style="margin: 0px; color: rgb(102, 0, 51);">-d</span> <span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">/</span>tmp<span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">/</span>selfextract.XXXXXX<span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">`</span> <span class="re2" style="margin: 0px; color: rgb(0, 120, 0);">ARCHIVE</span>=<span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">`</span><span class="kw2" style="margin: 0px; color: rgb(194, 12, 185); font-weight: bold;">awk</span> <span class="st_h" style="margin: 0px; color: rgb(255, 0, 0);">'/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }'</span> <span class="re4" style="margin: 0px; color: rgb(0, 120, 0);">$0</span><span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">`</span> <span class="kw2" style="margin: 0px; color: rgb(194, 12, 185); font-weight: bold;">tail</span> -n+<span class="re1" style="margin: 0px; color: rgb(0, 120, 0);">$ARCHIVE</span> <span class="re4" style="margin: 0px; color: rgb(0, 120, 0);">$0</span> <span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">|</span> <span class="kw2" style="margin: 0px; color: rgb(194, 12, 185); font-weight: bold;">tar</span> xzv <span class="re5" style="margin: 0px; color: rgb(102, 0, 51);">-C</span> <span class="re1" style="margin: 0px; color: rgb(0, 120, 0);">$TMPDIR</span> <span class="re2" style="margin: 0px; color: rgb(0, 120, 0);">CDIR</span>=<span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">`</span><span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">pwd</span><span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">`</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">cd</span> <span class="re1" style="margin: 0px; color: rgb(0, 120, 0);">$TMPDIR</span> .<span class="sy0" style="margin: 0px; color: rgb(0, 0, 0); font-weight: bold;">/</span>installer <span class="co0" style="margin: 0px; color: rgb(102, 102, 102); font-style: italic;"># 改为压缩包中安装程序的地址</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">cd</span> <span class="re1" style="margin: 0px; color: rgb(0, 120, 0);">$CDIR</span> <span class="kw2" style="margin: 0px; color: rgb(194, 12, 185); font-weight: bold;">rm</span> <span class="re5" style="margin: 0px; color: rgb(102, 0, 51);">-rf</span> <span class="re1" style="margin: 0px; color: rgb(0, 120, 0);">$TMPDIR</span> <span class="kw3" style="margin: 0px; color: rgb(122, 8, 116); font-weight: bold;">exit</span> <span class="nu0" style="margin: 0px; color: rgb(0, 0, 0);">0</span> __ARCHIVE_BELOW__
准备压缩文件
把程序的压缩包准备好即可,假设名为myfile.tar.gz
制作安装包
直接使用cat命令把两个文件连在一起就可以了:
$ cat inst_script.sh myfile.tar.gz > myinstaller $ chmod +x myinstaller
原理简介
下面给出带注释的代码
-
#!/bin/bash
-
echo ""
-
echo "Self Extracting Installer"
-
echo ""
-
-
# 用mktemp建立一个临时文件夹
-
export TMPDIR= ` mktemp -d /tmp /selfextract.XXXXXX `
-
-
# 找到压缩文件在包中的偏移量,即含__ARCHIVE_BELOW__的下一行
-
ARCHIVE= ` awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0 `
-
-
# 解压文件
-
tail -n+ $ARCHIVE $0 | tar xzv -C $TMPDIR
-
-
CDIR= ` pwd `
-
cd $TMPDIR
-
. /installer # 改为压缩包中安装程序的地址
-
-
cd $CDIR
-
rm -rf $TMPDIR
-
-
exit 0
-
-
__ARCHIVE_BELOW__