linux下打包成tar.gz,linux – 将tar.gz打包成shell脚本

本文介绍了如何将tar.gz文件打包成一个自解压的shell脚本,使得用户可以直接运行该脚本来解压和安装程序。通过在脚本中加入提取和安装的逻辑,并连接原始的tarball,可以生成一个名为run_me.sh的自解压程序。这个脚本首先创建一个输出目录,然后利用tail命令和awk找到payload的起始行号,将payload解压到指定目录。

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

我想知道如何将tar.gz文件打包成shell脚本,就像idk ** .bin一样.所以我可以在一个shell文件而不是tar.gz中提供程序

解决方法:

有一个Linux Journal article解释如何详细地做这个,包括有效载荷包装的代码等.正如Etan Reisner在他的评论中所说,提取/安装脚本知道如何削减其尾部以获得先前连接的有效载荷.这是一个如何工作的例子:

#!/bin/bash

# a self-extracting script header

# this can be any preferred output directory

mkdir ./output_dir

# determine the line number of this script where the payload begins

PAYLOAD_LINE=`awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' $0`

# use the tail command and the line number we just determined to skip

# past this leading script code and pipe the payload to tar

tail -n+$PAYLOAD_LINE $0 | tar xzv -C ./output_dir

# now we are free to run code in output_dir or do whatever we want

exit 0

# the 'exit 0' immediately above prevents this line from being executed

__PAYLOAD_BELOW__

注意使用$0来引用脚本本身.

要首先创建安装程序,您需要连接上面的代码和要安装/交付的tarball.如果上面的脚本名为extract.sh,并且有效负载名为payload.tar.gz,则此命令可以解决问题:

cat extract.sh payload.tar.gz > run_me.sh

标签:linux,shell,installer,installation,tar

来源: https://codeday.me/bug/20190824/1710959.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值