1

I have a python script which invokes two different shell scripts. The first script sets some environment variables which are required by the second script. The python code has the following structure :

import subprocess
subprocess.call(["bash", "a.sh"]) #a.sh sets env_var1
subprocess.call(["bash", "b.sh"]) #b.sh reads env_var1

Because the scripts a.sh and b.sh run in different shells, the above code does not do the needful. I want to know that can we execute these shell scripts from python in the current shell itself

user3282758
  • 1,379
  • 11
  • 29
  • 1
    Possible duplicate of [how to execute shell script in the same process in python](https://stackoverflow.com/questions/29621193/how-to-execute-shell-script-in-the-same-process-in-python). Short answer: seems like you can't. The workaround is to merge your scripts and run it in one process. – r.ook Oct 11 '18 at 13:18
  • Possible duplicate of [How to run multiple commands synchronously from one subprocess.Popen command?](https://stackoverflow.com/questions/39721924/how-to-run-multiple-commands-synchronously-from-one-subprocess-popen-command) – Wilmar van Ommeren Oct 11 '18 at 13:22

1 Answers1

1

You can use this line to run commands to your shell in python os.system('command to run here')

In your case it would be something like:

import os

os.system('./a.sh')
os.system('./b.sh')
ikuamike
  • 339
  • 1
  • 6
  • but how do I run both scripts in one `os.system` – user3282758 Oct 11 '18 at 13:19
  • the os.system doesn't leave the current shell, so running the one after the other will ensure the environment variables are created in the first script and accessible in the second – ikuamike Oct 11 '18 at 13:23
  • yes out of python it works. Following is my python script `path1 = r'd1/xyz.csh' path2 = r'd2/abc.csh' os.system("cat d1/xyz.csh") os.system(path1) os.system("echo $Env_var_1 sys1") os.system(path2)`. And `xyz.csh` is `Env_var_1="var one by xyz" Env_var_2="var two by xyz" echo $Env_var_1`. And `abc.csh` is `echo $Env_var_2, "in abc"` – user3282758 Oct 11 '18 at 13:37
  • use export when creating the variable – ikuamike Oct 11 '18 at 13:41
  • export also didnt help – user3282758 Oct 11 '18 at 13:46
  • I don't know the issue, `os.system` works the same as typing out the commands on the terminal. maybe try replicate the sequence you used on terminal with several os.system lines. Otherwise sorry I don't know – ikuamike Oct 11 '18 at 13:48
  • 4
    The issue is that `os.system` in fact does spawn a subprocess in which the subshell runs. It does so for every call to `os.system` which means you get a "fresh" process/subshell every time. Just try `os.system("export MYVAR=someval; echo $MYVAR")` and then `os.system("echo $MYVAR")`, or simply consecutive calls to `os.system("echo $$")`. – shmee Oct 11 '18 at 13:58
  • I'm not a python person, but does `os.system('./a.sh;./b.sh');` get you anything usable (assuming `export`ed var definitions in `./a.sh`).? Good luck to all. – shellter Oct 11 '18 at 14:37
  • Or `os.system('source ./a.sh; b.sh');` ? – Walter A Oct 11 '18 at 21:36