Shell 自解压安装脚步

本文介绍了如何制作Shell自解压安装脚本,包括利用sharutils工具和直接通过shell命令实现。通过示例说明了如何将文件归档、编码到脚本中,并在运行时解压和执行安装操作,适用于简化Linux平台的安装过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常在安装程序的时候直接通过一个 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 "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值