首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将tar.gz打包到shell脚本中

将tar.gz打包到shell脚本中
EN

Stack Overflow用户
提问于 2015-04-02 16:38:36
回答 2查看 6.2K关注 0票数 7

我想知道如何像tar.gz一样将一个idk**.bin文件打包到一个shell脚本中。所以我可以用一个shell文件而不是tar.gz来传递程序。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-02 17:07:27

有一个Linux杂志文章详细解释了如何做到这一点,以及打包有效载荷的代码等等。正如earlier在他的评论中所说的那样,提取/安装脚本知道如何切断它的尾部以获得之前连接的有效负载。下面是一个如何工作的例子:

代码语言:javascript
复制
#!/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,那么这个命令将完成以下任务:

代码语言:javascript
复制
cat extract.sh payload.tar.gz > run_me.sh
票数 13
EN

Stack Overflow用户

发布于 2015-04-03 14:29:23

你也可以这样做:

代码语言:javascript
复制
#!/bin/bash
BASEDIR=`dirname "${0}"`
cd "$BASEDIR"

payload=$1
script=$2
tmp=__extract__$RANDOM

[ "$payload" != "" ] || read -e -p "Enter the path of the tar archive: " payload
[ "$script" != "" ] || read -e -p "Enter the name/path of the script: " script

printf "#!/bin/bash
PAYLOAD_LINE=\`awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' \$0\`
tail -n+\$PAYLOAD_LINE \$0 | tar -xvz
#you can add custom installation command here

exit 0
__PAYLOAD_BELOW__\n" > "$tmp"

cat "$tmp" "$payload" > "$script" && rm "$tmp"
chmod +x "$script"

如果将此文件保存为t2s,则可以这样使用:

代码语言:javascript
复制
t2s test.tar.gz install.sh

运行install.sh将提取当前目录中的内容。如果需要,也可以运行自定义安装脚本。您必须正确地将它们添加到printf部分中。

如果您需要对其他压缩类型(.tar.bz2等)执行此操作,则需要在本节中编辑z选项:

代码语言:javascript
复制
tail -n+\$PAYLOAD_LINE \$0 | tar xzv
#it's inside a quote and $ needs to be printed, so you will need to use \

例如:

对于.tar.bz2:

代码语言:javascript
复制
tail -n+\$PAYLOAD_LINE \$0 | tar xjv 
#it's inside a quote and $ needs to be printed, so you will need to use \

对于.tar

代码语言:javascript
复制
tail -n+\$PAYLOAD_LINE \$0 | tar xv 
#it's inside a quote and $ needs to be printed, so you will need to use \

有关此选项的信息,您可以看到tar的手册页:

代码语言:javascript
复制
man tar

我已经把它变成了一个工具来自动化这些任务。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29418050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档