经常在安装程序的时候直接通过一个 shell 脚步就完成了,个人觉得这种方式也是挺方便的。于是就研究了一下,发现其实实现起来也并不复杂,本文主要记录一下整个制作过程。
我目前了解到的 shell 自解压的安装脚步制作方式分为两种:
- sharutils - 利用 sharutils 工具对多个文件进行归档,同时生成一个 shell 脚步用于取出这些文件。该脚步中包含编码后的二进制或压缩包等文件的信息以及解码操作。这种方式需要依赖于 sharutils 工具,不够灵活。
- 自己通过 shell 命名实现 - 这种方式几乎能在所有的 Linux 平台运行,不依赖于特定工具。
为了简化操作,我采用了一个简单的 helloworld 工程目录,然后利用脚步对其进行解压并对该工程进行编译来模拟安装。
$ tree helloworld
helloworld/
main.c
Makefile
$ cat helloworld/main.c
#include <stdio.h>
int main()
{
printf("Hello world!\n");
return 0;
}
$ cat helloworld/Makefile
.PHONY: all
all: helloworld
helloworld: main.o
gcc -o $@ $^
%.o: %.c
gcc -o $@ -c $<
.PHONY: clean
clean:
rm -rf *.o helloworld
通过修改编译过程为安装过程即可实现脚步自动话安装。这里就只提供思路,不具体给出安装的示例了,希望大家可以举一反三。
Sharutils
Sharutils 是从许多文件中制作所谓的 shell 档案,准备通过电子邮件服务传输的工具。本文主要使用 shar 命令对多个文件进行归档,并调用其中的 shell 脚步执行基本的操作。在这之前需要首先安装 sharutils 工具集,在 Ubuntu 下使用如下命令:
$ sudo apt-get install -y sharutils
首先,我们对 helloworld 进行压缩
$ tar -czvf helloworld.tgz helloworld
helloworld/
helloworld/main.c
helloworld/Makefile
接着我们使用 shar 对压缩包进行编码并重定向到 compile.sh 脚步中
$ shar helloworld.tgz > compile.sh
shar: Saving helloworld.tgz (text)
$ cat compile.sh
#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.15.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the '#!/bin/sh' line above, then type 'sh FILE'.
#
lock_dir=_sh11831
# Made on 2017-10-13 10:25 CST by <japin@ww-it>.
# Source directory was '/home/japin/WwIT/Codes/self-extract'.
#
# Existing files will *not* be overwritten, unless '-c' is specified.
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 344 -rw-rw-r-- helloworld.tgz
#
MD5SUM=${MD5SUM-md5sum}
f=`${MD5SUM} --version | egrep '^md5sum .*(core|text)utils'`
test -n "