0

I would like to know if it is possible to call a shell script from a python program but the shell script is not in the same directory that the python file? For the moment I just succeed in calling a shell script which is in the same directory that the python file which calls it thanks to this command:

So basically, in the program I have to do, I can't change the location of my bash script, so I have to find a way to call this bash script from my python file (qnd there are not in the same path)>

subprocess.call(["./my_shell_script.sh",my_argument])                                                                             

Thank you,

SOLUTION:

subprocess.call(["the/path/to/the/script/script.sh"])

Jordane

  • I suspect this isn't really a duplicate. However, I must confess this in not quite clear to me. You know how to call a shell script in the current working directory. You know how to call it given its full path. From you comments, you seems to know how to change the PATH. Are you looking for something like "searching the executable path?" You will probably have to edit you question to show a broader view of your problem. – Sylvain Leroux Jul 18 '14 at 13:36
  • I brought a modification as you advised me. As you said, I know how to call shell script if it is in the same working directory than my python file. But the problem is to know how to call it if it is not in the same directory –  Jul 18 '14 at 13:45
  • Thank you for taking time editing your question. My _guess_ is you are really looking to launch a script *relative* to the current working directory. Could you give us the path of you Python script *and* the Shell script you're attempting to launch? – Sylvain Leroux Jul 18 '14 at 13:49
  • The solution was "obvious", I edit my post! Thank you a lot for your help! –  Jul 18 '14 at 14:01

3 Answers3

1

You should always pass the full path to the script:

import shlex
import subprocess

cmd = '/bin/bash /home/jordane/scripts/script.sh -opt 1 arg1 arg2'
subprocess.call(shlex.split(cmd))
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I tried but it doesn't work: I wrote: cmd = '/bin/bash /home/path/to/my/file/script.sh' subprocess.call(shlex.split(cmd)) But I have the error: /bin/bash: /home/path/to/my/file/script.sh: No such file or directory –  Jul 18 '14 at 12:28
  • But it works if the path to acceed at my bash script '/bin/bash /home/path/to/my/file/script.sh' is the same path for my python program, otherwise, it doesn't work. –  Jul 18 '14 at 12:33
1

./my_shell_script.sh means "my_shell_script in the current working directory".

Have you tried to replace that by the absolute path to your script?

subprocess.call(["/full/path/to/my_shell_script.sh",my_argument])

Or, if your script path is in your current PATH environment variable:

subprocess.call(["my_shell_script.sh",my_argument])

Or again (still if your script path is in your current PATH):

subprocess.call(["bash", "-c", " ".join("my_shell_script.sh",my_argument)])

From the above, first and second solution requires a proper shebang (#!/bin/bash) as the first line of the script. The third solution does not have this "limitation".


EDIT:

If your are looking to launch a script whose path is relative to the current working directory, you have to provide a relative path. This allows both your Python script and your Shell script to be moved around as long as their relative positions does not change.

For example, if you script is in the sub-directory my_subdir of the current working directory, you might write:

subprocess.call(["my_subdir/my_shell_script.sh",my_argument])

You also have to opportunity to navigate up one level by using ... And you could use that several times to move up several levels. For example:

subprocess.call(["../../a/b/c/my_shell_script.sh",my_argument])

That above example will launch my_shell_script in the a/b/c sub-directory starting two levels above the current working directory.

Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • first solution: it doesn't work if the script is in another path second solution: this is what I use to launch the script if it is in the same directory than my python file. thirth solution: I didn't try because where can I write the path to launch the script? Thanks anyway but I'm still blocked :) –  Jul 18 '14 at 13:28
  • @Jordane Please take some time reviewing my answer. Especially the _edit_ about relative path. I guess this is what you are looking for! – Sylvain Leroux Jul 18 '14 at 14:02
  • @ Sylvain Leroux Yes Sylvain, it was what I'm looking for ;) It's your answer subprocess.call(["my_subdir/my_shell_script.sh",my_argument]) which give me the solution! Thx! However, it seems that it doesn't work when I try to put some argument.. –  Jul 18 '14 at 14:06
0

If you get stuck, you can open a whole process:

p = subprocess.Popen(['/path/to/script','arg1','arg2'], stdout=subprocess.PIPE)
stdout,stderr = p.communicate()

You can also set it to shell mode. This way you can let the OS handle things the way you may be more familiar with.

I assume your path doesn't have any weird characters in it?

BSAFH
  • 725
  • 3
  • 6
  • 19
  • With this solution, my script is not read anymore... And yes, the path doesn't have weird characters ;) –  Jul 18 '14 at 13:34
  • Just with regards to your comments to Sylvain, make sure if an argument requires a "-" that you include that. e.g. ["/path/to/script","-u","username","-p","password"]. Also make sure to mark his answer as the answer. This helps future users but it is also the most polite thing to do given that he took the time to answer you. You might be able to upvote him at the same time. – BSAFH Jul 18 '14 at 23:13