我有一个脚本可以运行一些外部命令并返回它们的值。我能够在 IDLE 中正常运行脚本,并且得到了预期的结果。
当我在 Linux shell (Bash) 中执行脚本时,它会运行,但没有输出。
退出状态为0。
#!/usr/bin/python
import array,os,subprocess
def vsdIndex(vmVsd):
index = subprocess.call(["/root/indeces.sh", vmVsd], stdout=subprocess.PIPE, shell=TRUE).communicate()[0]
print index
return (firstSerial,lastSerial)
def main():
vsdArray = []
vsdProc = subprocess.Popen(["sc vsd show | tail -n +2 | grep -Ev 'iso|ISO|vfd' | awk '{{print $1}}'"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
while True:
line = vsdProc.stdout.readline()
if line != '':
vsdIndex(line)
print "VSD:", line.rstrip()
print firstSerial
print lastSerial
else:
break
如果我简化它,并且在没有函数的情况下运行,我会有相同的行为:
def main():
vsdArray = []
vsdProc = subprocess.Popen(["sc vsd show | tail -n +2 | grep -Ev 'iso|ISO|vfd' | awk '{{print $1}}'"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
while True:
line = vsdProc.stdout.readline()
if line != '':
print "VSD:", line.rstrip()
else:
break
最佳答案
您需要调用 main() 函数。这是在命令行上运行时自动执行此操作的常用方法:
if __name__ == "__main__":
main()
关于python - 脚本在 IDLE 中返回输出,而不是在 CLI 上执行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36699510/