0

This is how I running the bash script set in myscript.sh

#!/bin/bash
import subprocess
child = subprocess.Popen(['bash', '-c', '/bin/bash myscript.sh'], stdout = subprocess.PIPE)
output=child.communicate()
print(output)

myscript.sh

Why am I making this is because the bash script that I have to run does not have the export command so I am echoing here. Code bellow. I know echoing wont set the environment variable but I just want to echo it and fetch the value so echoing instead of exporting.

#!/bin/bash
source ia_servers
echo $IA_SRV_cs68_64

Where, $IA_SRV_cs68_64

Here is the ia_servers file where the variable values are listed it looks something like this

IA_SRV_cs68_64="ds1 ds2 ds3 ds5 "

This is how the variables are in it. Many other variable are set in it eliminated because too longer.

TESTED works well from terminal:

  • source ia_servers
  • echo IA_SRV_cs68_64

prints the desired values set

Problem: Though myscript.sh is working and printing the variables . What I want is I don't want to create a separate file write a bash script there to echo the variable as in the myscript.sh but instead write it within the python.

How do I accomplish it within the python rather rather than making the script file and run that.

Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76

2 Answers2

0

Here's a universal way to run any kind of script or program where program input is embedded in the Python script.

import subprocess
scriptres = subprocess.Popen("/bin/bash", 
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE).communicate(r'''
# your bash script here
ls
pwd
source whatever
echo $SOMETHING
''')

script_stdout = scriptres[0]
script_stderr = scriptres[1]
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0
# i assume there is a file by name `ia_servers` with variable IA_SRV_cs68_64="ds1 ds2 ds3 ds5 "

import imp
ia_servers =imp.load_source('ia_servers', 'path to `ia_servers` file')

print ia_servers.IA_SRV_cs68_64 # this should print `ds1 ds2 ds3 ds5` 
Keval
  • 97
  • 1
  • 9
  • I think you can do this even without using a bash script. is this what you want? – Keval Jun 07 '17 at 08:06
  • That file path has the environment source. Want to load that and echo our the variables . TRYING this approach says AttributeError: 'module' object has no attribute 'IA_SRV_cs68_64' – Tara Prasad Gurung Jun 07 '17 at 09:00
  • This error is because you were not able to import the ia_servers, I had misspelt it last time. – Keval Jun 07 '17 at 09:18
  • I tried with correct names. >>> import imp >>> ias_server=imp.load_source('ias_servers',' ./ ') #alsotried with' .' >>> print(ias_server.IA_SRV_cs68_64) Still same – Tara Prasad Gurung Jun 07 '17 at 09:40
  • checked with dir(). It has no such attributes. Also checked if it is a path issue since the ias_servers is in the same path i tried '.' and also './' no effects – Tara Prasad Gurung Jun 07 '17 at 09:43
  • yes i tested with proper variable in before . the variable i don't think need to be same as the fie name i have added my code in comment above – Tara Prasad Gurung Jun 07 '17 at 09:59
  • I got it but your file should be same – Keval Jun 07 '17 at 10:02
  • in your comment your file name is ias_servers were as in question it says ia_servers – Keval Jun 07 '17 at 10:03
  • Tried again, same problem that attribute does not exist. The problem comes in print statement . No such attribute exist. Means the load_source was success but it doesnot have anything to do with IA_SRV_cs68_64 – Tara Prasad Gurung Jun 07 '17 at 10:05
  • change this ias_server=imp.load_source('ias_servers',' ./ ') to ias_server=imp.load_source('ias_servers',' ./ia_servers') you need to give full path ,second argument is path upto file, also try giving absolute path of the file. – Keval Jun 07 '17 at 10:09
  • Now I am getting different errors not in our code but in the basscript file – Tara Prasad Gurung Jun 07 '17 at 10:16
  • why do you need as bash script – Keval Jun 07 '17 at 10:18
  • Thats where the server list and groups are set which my python application can access – Tara Prasad Gurung Jun 07 '17 at 10:19
  • what i just want to accomplish is what is successfully done from the terminal Getting the values. GETTING some syntax related errors – Tara Prasad Gurung Jun 07 '17 at 11:41
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 07 '17 at 11:43