从蟒蛇内运行bash脚本 [英] Running bash script from within python

查看:147
本文介绍了从蟒蛇内运行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code的一个问题:

I have a problem with the following code:

callBash.py:

import subprocess
print "start"
subprocess.call("sleep.sh")
print "end"

sleep.sh:

sleep 10

我想10秒后要被打印的结束。 (我知道这是一个愚蠢的例子,我可以简单地蟒蛇内睡觉,但这个简单的sleep.sh文件只是作为测试)

I want the "end" to be printed after 10s. (I know that this is a dumb example, I could simply sleep within python, but this simple sleep.sh file was just as a test)

推荐答案

制作sleep.sh可执行文件并加入壳= TRUE 参数列表(如建议在previous答案)工程确定。根据不同的搜索路径,您可能还需要添加 ./ 或其他一些合适的路径。 (也就是说,变化sleep.sh./ sleep.sh

Making sleep.sh executable and adding shell=True to the parameter list (as suggested in previous answers) works ok. Depending on the search path, you may also need to add ./ or some other appropriate path. (Ie, change "sleep.sh" to "./sleep.sh".)

SHELL =不需要真参数(POSIX的系统如Linux下)如果bash脚本的第一行是一个shell的路径;例如,#!/斌/庆典

The shell=True parameter is not needed (under a Posix system like Linux) if the first line of the bash script is a path to a shell; for example, #!/bin/bash.

这篇关于从蟒蛇内运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆