文件组合成一个可执行文件以进行部署

文件组合成一个可执行文件以进行部署

本文介绍了将 shell 脚本和 zip 文件组合成一个可执行文件以进行部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件要部署,

1) deploymentpackage.zip -> 包含数据库包,shell 脚本很少.

1) deploymentpackage.zip -> It contains the database package with few shell scripts.

2) deployment.sh -> 它是主要的shell 脚本,它首先解压缩deploymentpackage.zip,然后执行其中的shell 文件列表.

2) deployment.sh -> It is the primary shell script which first unzips the deploymentpackage.zip and then execute the list of shell files inside it.

它按预期工作.

但我需要的是,我需要将 zip 文件设为可执行文件,这样我就不想将 deploymentpackage.zipdeployment.sh 都交付给客户端.

But what I need is, I need to make the zip file as executable so that I dont want to deliver both deploymentpackage.zip and deployment.sh to client.

那么是否可以将 deploymentpackage.zip 设为可执行文件,这样我就不想再有一个脚本 deployment.sh.

So Is it possible to make the deploymentpackage.zip as executable so that I don't want to have another script deployment.sh.

期望:运行这个 deploymentpackage.zip 应该解压缩同一个文件并运行其中的脚本列表.

Expectation : Running this deploymentpackage.zip should unzip the same file and run the list of scripts inside it.

推荐答案

如果可以假设将运行脚本的用户具有 unzip 实用程序,那么您可以创建这样的脚本:

If it's ok to assume that the user who will run the script has the unzip utility, then you can create a script like this:

#!/usr/bin/env bash

# commands that you need to do ...
# ...

unzip <(tail -n +$((LINENO + 2)) "$0")
exit

确保脚本在exit 行的末尾有一个换行符.而且,重要的是脚本的最后一行是 exit 命令,并且带有 tailunzip 命令就在它的前面.

Make sure the script has a newline character at the end of the line of exit. And, it's important that the last line of the script is the exit command, and that the unzip command with tail is right in front of it.

然后,您可以将压缩的内容附加到此文件中,例如:

Then, you can append to this file the zipped content, for example with:

cat file.zip >> installer.sh

用户将能够运行 installer.sh,它会解压缩文件末尾的压缩内容.

Users will be able to run installer.sh, which will unzip the zipped content at the end of the file.

这篇关于将 shell 脚本和 zip 文件组合成一个可执行文件以进行部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:12