2

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.

  • shar(1) would be traditional, but why does it need to be a single executable, when you could instead use rsync or git or something to move a file tree around as needed? – thrig Jun 1 '16 at 21:56
  • For convenience. For example, when I want to run the script from /usr/local/bin, I don't want to move the whole directory to /usr/local/bin - I think it would be more convenient to move just the combined executable. – UCLA Jun 1 '16 at 22:07
  • This sounds messy and fragile. You shouldn't be moving scripts around like that, and definitely not into directories owned by root, unless you know exactly what you're doing. Why can't you just pass the directory you want the script to work on as a command-line argument? Are you aware of the pwd 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:10
  • 1
    FWIW, depending on your system, a script on your PATH may be able to determine the full name of the directory it is stored in (no matter what your current directory is) by doing, eg home=$(dirname $(readlink -e $0)), but hopefully you shouldn't need to do that. – PM 2Ring Jun 1 '16 at 23:11
  • I can and I am aware of ways to get a bash script to use a directory. For exampe when running cron jobs I just cd into the desired directory. Problem is that this is hardcoded and I need to change it everytime I move to a new system. Was just wondering if there was a more elegant way so that I could combine everything into one file (executable) and just move that thing around anyway I want without having to change things every time. – UCLA Jun 2 '16 at 0:04
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.