简体   繁体   English

我可以在当前shell中从python脚本运行shell命令吗

[英]Can I run a shell command from python script in the current shell

I have a python script which invokes two different shell scripts. 我有一个python脚本,它调用了两个不同的shell脚本。 The first script sets some environment variables which are required by the second script. 第一个脚本设置了第二个脚本所需的一些环境变量。 The python code has the following structure : python代码具有以下结构:

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. 因为脚本a.shb.sh在不同的壳运行,上面的代码不会做要紧。 I want to know that can we execute these shell scripts from python in the current shell itself 我想知道我们可以在当前shell本身中从python执行这些shell脚本吗

You can use this line to run commands to your shell in python os.system('command to run here') 您可以使用此行在python os.system('command to run here')向外壳运行命令os.system('command to run here')

In your case it would be something like: 在您的情况下,它将类似于:

import os

os.system('./a.sh')
os.system('./b.sh')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM