python - 脚本在 IDLE 中返回输出,而不是在 CLI 上执行时

标签 python linux bash

我有一个脚本可以运行一些外部命令并返回它们的值。我能够在 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/

相关文章:

node.js - Bash 导出变量但仅适用于当前命令

bash - 通过 SSH 连接到 guest 计算机后继续执行 shell 脚本吗?

python - 在 Python 解释器中调试代码

linux - 查找名称与多种模式之一匹配的文件并复制到不同的目录

python - if A vs if A is not None :

调用 adb start-server 时 Linux 系统死机

python - 降级python版本

bash - 我无法使用基于 ubuntu 的镜像部署 Docker Swarm 服务

c# - 从 Python 运行 .NET COM 程序集。程序集 dll 的 Python 路径?

python - 转换字节->字符串->返回字节,并获取原始值