I have been trying to use the sub-process module to run a sequence of shell commands in succession, to automate certain tasks using python(for the first time). The following are the steps involved in the task mentioned before:
- Goto location 1
- Execute the cshell script in the path location
- Goto location 2
- Execute another cshell script in the new path location
I have written the following code for the purpose:
#! /usr/bin/python
import subprocess
import os
path = r'/AA/BB/CC/DD/EE/FF/GG/HH/'
os.chdir(path)
p1 = subprocess.Popen('./xyz.csh', stdout=subprocess.PIPE, shell=True)
outdata = p1.communicate()[0]
os.chdir('../XX/YY/')
subprocess.call("./abc.csh")
print("Simulation complete")
The script file contains a few setenv commands which set the environment variables that are required for the execution of abc.csh script. I have the following query about the usage of subprocess.Popen() routine, as the subprocess.Popen() routine creates a sub-shell for the execution of xyz.csh, the environment variables set by the execution of the script vanish once the execution is completed, and abc .csh (which requires those environment variables for execution) throws errors during execution. Is there a way to set environment variables in main shell so that abc.csh can be executed with fail.