我正在编写一个脚本来从指定的路径中提取某些东西。我将这些值返回给一个变量。我如何检查shell命令是否返回了一些或什么都没有。如何检查一个shell命令是否返回什么或什么
我的代码:
def any_HE():
global config, logger, status, file_size
config = ConfigParser.RawConfigParser()
config.read('config2.cfg')
for section in sorted(config.sections(), key=str.lower):
components = dict() #start with empty dictionary for each section
#Retrieving the username and password from config for each section
if not config.has_option(section, 'server.user_name'):
continue
env.user = config.get(section, 'server.user_name')
env.password = config.get(section, 'server.password')
host = config.get(section, 'server.ip')
print "Trying to connect to {} server.....".format(section)
with settings(hide('warnings', 'running', 'stdout', 'stderr'),warn_only=True, host_string=host):
try:
files = run('ls -ltr /opt/nds')
if files!=0:
print '{}--Something'.format(section)
else:
print '{} --Nothing'.format(section)
except Exception as e:
print e
我试图检查1或0,是真是假,但似乎没有奏效。在某些服务器中,路径“/ opt/nds /”不存在。所以在这种情况下,文件上没有任何东西。我想区分返回到文件的东西,而不是返回到文件。
究竟是“运行”? – poke
您需要使用'subprocess'模块来管理您的流程。看看[subprocess documentation](https://docs.python.org/2/library/subprocess.html)。 – perror
你的缩进被破坏,这段代码永远不会运行。 – 2016-02-12 11:22:13