我想知道如何像tar.gz一样将一个idk**.bin文件打包到一个shell脚本中。所以我可以用一个shell文件而不是tar.gz来传递程序。
发布于 2015-04-02 17:07:27
有一个Linux杂志文章详细解释了如何做到这一点,以及打包有效载荷的代码等等。正如earlier在他的评论中所说的那样,提取/安装脚本知道如何切断它的尾部以获得之前连接的有效负载。下面是一个如何工作的例子:
#!/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发布于 2015-04-03 14:29:23
你也可以这样做:
#!/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,则可以这样使用:
t2s test.tar.gz install.sh运行install.sh将提取当前目录中的内容。如果需要,也可以运行自定义安装脚本。您必须正确地将它们添加到printf部分中。
如果您需要对其他压缩类型(.tar.bz2等)执行此操作,则需要在本节中编辑z选项:
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:
tail -n+\$PAYLOAD_LINE \$0 | tar xjv
#it's inside a quote and $ needs to be printed, so you will need to use \对于.tar
tail -n+\$PAYLOAD_LINE \$0 | tar xv
#it's inside a quote and $ needs to be printed, so you will need to use \有关此选项的信息,您可以看到tar的手册页:
man tar我已经把它变成了一个工具来自动化这些任务。
https://stackoverflow.com/questions/29418050
复制相似问题