linux的制作自解压缩包,并运行程序

基本思路是创建一个文件,前半部分是一个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


原理简介

下面给出带注释的代码

  1. #!/bin/bash
  2. echo  ""
  3. echo  "Self Extracting Installer"
  4. echo  ""
  5.  
  6. # 用mktemp建立一个临时文件夹
  7. export  TMPDIR= ` mktemp  -d  /tmp /selfextract.XXXXXX `
  8.  
  9. # 找到压缩文件在包中的偏移量,即含__ARCHIVE_BELOW__的下一行
  10. ARCHIVE= ` awk  '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }'  $0 `
  11.  
  12. # 解压文件
  13. tail -n+ $ARCHIVE  $0  |  tar xzv  -C  $TMPDIR
  14.  
  15. CDIR= ` pwd `
  16. cd  $TMPDIR
  17. . /installer    # 改为压缩包中安装程序的地址
  18.  
  19. cd  $CDIR
  20. rm  -rf  $TMPDIR
  21.  
  22. exit  0
  23.  
  24. __ARCHIVE_BELOW__
原文: http://linux-wiki.cn/wiki/zh-hans/%E5%88%B6%E4%BD%9C%E8%87%AA%E8%A7%A3%E5%8E%8B%E5%AE%89%E8%A3%85%E6%96%87%E4%BB%B6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值