Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

linux - Combine a shell script and a zip file into a single executable for deployment

I have two files for deployment,

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

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.

It is working as expected.

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...