我有一个python脚本(使用伪终端)来传递一个名为" CDP"的环境变量:
def download(self, dPluzz, donnees=None): # to call the bash script
self.child_pid = self.v.fork_command(None, ['/bin/bash', 'dPluzz-cli', '-f', dest, '-u', adresse])
os.environ["CDP"] = "False" # set cancel as "False"
def cancel(self, dPluzz, donnees=None):
if self.annul == 0:
if self.time > 10 and self.percent != 100:
os.environ["CDP"] = "True"
print os.environ["CDP"] # returns True
self.child_pid = str(self.child_pid)
cmd = 'kill -TERM' + " " + self.child_pid
subprocess.Popen(cmd, shell=True)
def __init__(self): #Pseudo-Terminal in GTK window
self.v = vte.Terminal() #(displayed in a notebook)
self.v.connect ("child-exited", lambda term: self.verif(self, a))
self.v.connect('contents-changed', self.term)
self.v.set_size(70,20)
self.v.set_encoding("UTF-8")
self.v.set_cursor_blinks(False)
self.v.show()
self.page.add(self.v)
bash脚本是:
kill_jobs()
{
pkill -TERM -P "$BASHPID"
echo -e "$CDP" # returns False, should be True
if [ "$CDP" == "True" ]; then
echo -e "OPERATIONS ANNULEES"
elif [ "$CDP" == "False" ]; then
echo -e "OPERATIONS TERMINEES"
fi
}
问题是,$CDP = False
所以显示的消息不好。
是什么原因?
感谢
答案 0 :(得分:0)
通过
设置环境后os.environ["CDP"] = "True"
只有通过os.system(), os.popen() or os.fork() and os.execv().
所以如果你可以添加
os.system('/bin/bash script.sh')
您通常可以在bash脚本中使用CDP的值。
请阅读os.putenv()
我猜os.environ和os.putenv()密切相关。