When trying to execute a bash script, it is often execute inside some directory, and requires access to local resources. I was wondering if there was a way to combine both the bash script and the resources (everything else in the directory) into an executable, so that I would only need that executable stand-alone to execute the bash script.
You probably should not combine a Bash script into an executable, for these reasons, but if you absolutely need an executable, you can use SHC (technically an obfuscation algorithm). SHC "translates" the script into C code, and then compiles it.
We then come to the problem of the multiple files. Because SHC can only take one file as input, you will have to combine all the files into one. Obviously this is not possible, because if it was, you would have already done that, but, you can move all the other files to one location, and then have the main program call those files.
An alternative
Another more advisable alternative is to move all the needed files into one directory, and then write a separate program that calls them all. For example, if your program requires three files, x
, y
, and z.txt
, you can write another script, n
, that calls x
and y
. Both x
and y
can call z.txt
, because they are in the same directory.
Simply mark the main program (n
in the example) as executable, and then move it to whatever location you need it to be in. Because the location of the needed files do not change, no matter where you move the main file, it can always access the others.
This inevitably falls short of your intended goal, but is as close as possible. If you need the "needed files" to access a file wherever you move the program to, you can simply have the main program pass that file's name as a command line argument. If needed, you can write the main program in C so that it is an executable binary file, although I realize that was not your original goal, may conserve time and space in the calling procedures.
shar(1)
would be traditional, but why does it need to be a single executable, when you could instead usersync
orgit
or something to move a file tree around as needed? – thrig Jun 1 '16 at 21:56pwd
command? Also, you can store the name of the directory containing the resources your script needs in a variable in the script, or pass it in on the command-line. – PM 2Ring Jun 1 '16 at 23:10home=$(dirname $(readlink -e $0))
, but hopefully you shouldn't need to do that. – PM 2Ring Jun 1 '16 at 23:11