0

I have multiple bash scripts and resources required by these scripts (like music files and images) and I want to pack them all into one executable file.

For example:

$ find Scripts/

Scripts/ Scripts/main.sh Scripts/bin/1.sh Scripts/bin/2.sh Scripts/src/music.mp3

Now, I want to pack all these into one executable which if I execute, executes the Scripts/main.sh file.

How can I do that?

Spiff
  • 110,156

1 Answers1

2

One way is to use the Shell Archive (shar) utility. It is maybe not installed by default, but likely present in the repository of your distribution.

Very much like tar it can create an single-file archive of a list of files. The difference is that the archive file is self-extracting: this is a shell script that has to be run.

In your case :

shar -M -D -Q Scripts/ > myarchive.sh packs the whole Scripts/ folder into myarchive.sh.

Then you can pass myarchive.sh to someone else. By running sh myarchive.sh he will locally unpack Scripts/ and all its content.

But this is not enough, as you also want to execute Scripts/main.sh . You have then to add this command at the end of myarchive.sh, and the full command is:

shar -M -D -Q Scripts/ | head -n -1 > myarchive.sh && echo "Scripts/main.sh" >> myarchive.sh

The purpose of head -n -1 is to remove the last line of the archiv, which is exit 0, otherwise the command you are adding won't be executed. I haven't tested since this syntax doesn't work on macOS (I used grep -v "exit 0"instead, but there can be edge effects with that).

Now Scripts/main.sh will be executed once the archive is unpacked.

I couldn't solve a (small) problem: the -Q option is supposed to make the unpack stage quiet, without output messages. But there are still some of them, I don't why...

PierU
  • 1,742