0

I have a couple of bash scripts that I use in a Python program. The current solution is to browse to the file in the GUI and then run the script(s) like this (pathToScript is the String for the path to the script):

INPUT = [pathToScript, input1, input2, input3, input4, input5, input6, input7, input8, input9, input10]
subprocess.call(INPUT)

To avoid always browsing to the script or hard coding the path, I am looking for a solution to somehow include the bash script in the compiled Python program and then run it inside the program. Is this possible? I'd rather not rewrite the bash scripts to Python if I can avoid it.

1
  • I think at some point, you have to call the bash, if you want to execute a bash-script. But you could also use bash -c "echo 'this works'" Commented Mar 20, 2013 at 10:40

2 Answers 2

3

You can always bundle the bash script with the rest of your program (like I suspect you are already doing), and reconstruct the path to these scripts at runtime.

It is easy enough to get the location of any python module from within the module itself, then create path to the bash script from there:

import os.path

LOCATION = os.path.dirname(os.path.abspath(__file__))
pathToScript = os.path.join(LOCATION, 'bashscript.sh')

and pathToScript would then be an absolute path to the file bashscript.sh sitting in the same directory as the current Python module.

Sign up to request clarification or add additional context in comments.

5 Comments

That doesn't include the bash script in the 'compiled python program' as requested by the OP
@Anthon: No, this addresses the problem the OP is facing. There is no need to store the bash program in a string here. There are multiple bash scripts involved, they are already included with the program, the only problem is finding the path to them when the program has been installed. This answer addresses that problem.
Your answer is a possible solution to the problem, but not an answer to the question he poses. It is possible to include the bash script in the compiled Python program.
@Anthon: No, the OP is casting around for a solution to a problem, and mentioned one vague way they thought of. That does not mean that that literal implementation is the best solution to this problem.
Thanks, this is a neat solution. And, yes, I was casting around for a solution. Putting the text in a string variable is possible, of course, but for testing and editing purposes I think this solution is better.
-2

You can put the actual text of each bash script in a multi-line string variable, then write each of these out to a file. You will know where you write the file, so you can invoke the scripts.

Make sure you set the execute permission of the scripts, or alternatively explicitly insert the path to bash as the first item in INPUT

2 Comments

This is a "technically alright," but heavy-handed approach. What happens when he wants to add a script? Remove a few? Setting the path to the current directory of his scripts is still hardcoding, but is substantially more lightweight and maintainable than loading up a Python variable with a copy/paste bash script.
@tristan: I am not sure if this is heavy handed. The OP clearly wants to have all in one place: the python script. That makes it e.g. easy to deploy, e.g. if multiple servers are to be populated. Of course there are other solutions for that as well. I would not call that heavy handed. Writing out the scripts would be done on demand and you could keep old scripts around by just keeping the string variables of newer ones in a list (that also determines the execution order).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.