【问题标题】:How to read python variables into bash command [closed]如何将python变量读入bash命令[关闭]
【发布时间】:2020-04-12 17:46:55
【问题描述】:

我正在编写脚本以使用 Python 2.6 生成 Autosys 报告,并且我想将变量从 python 脚本传递到 bash 命令:

我有 3 个变量:

下个月, ND下个月, 年份

当我使用带有 1 个变量的命令时,它可以正常工作。

    env = os.environ.copy()
env['NextMonth'] = NextMonth
subprocess.call('forecast -J *JobABC_* -M ALL -F "${NextMonth}/01/2020 00:00" -T "${NextMonth}/31/2020 23:59" -h -n > PythonReport1.txt', env=env, shell=True)

反之则不行,日期无效:

    env = os.environ.copy()
env['NextMonth'] = NextMonth
env['NDNextMonth'] = NDNextMonth
env['Year'] = Year
subprocess.call('forecast -J *JobABC_* -M ALL -F "${NextMonth}/01/${Year}" 00:00" -T "${NextMonth}/${NDNextMonth}/${Year}" 23:59" -h -n > PythonReport1.txt',env=env, shell=True)

您能否检查一下,如何将这 3 个变量读入命令中? 错误:TypeError:execve() arg 3 包含非字符串值

【问题讨论】:

  • ...您为什么首先为此使用环境变量?将值插入字符串不是更有意义吗?
  • 我想你想用 ` "${'+str(NextMonth)+'}/01/'` 来结束字符串?
  • 你在那个字符串中有一些额外的双引号。试试看:subprocess.call('forecast -J *JobABC_* -M ALL -F "${NextMonth}/01/${Year} 00:00" -T "${NextMonth}/${NDNextMonth}/${Year} 23:59" -h -n > PythonReport1.txt',env=env, shell=True)
  • 已解决,它的工作原理是这样的: os.environ['NextMonth'] = str(NextMonth) os.environ['NDNextMonth'] = str(NDNextMonth) os.environ['Year'] = str(Year) subprocess.call('预测 -J JobABC_ -M ALL -F "${NextMonth}/01/${Year}" 00:00" -T "${NextMonth}/ ${NDNextMonth}/${Year}" 23:59" -h -n > PythonReport1.txt', shell=True)

标签: python linux bash python-2.6


【解决方案1】:

正如 cmets 中所指出的,您的变量工作正常;问题是字符串中有虚假的双引号。

subprocess.call('forecast -J *JobABC_* -M ALL'
    ' -F "${NextMonth}/01/${Year} 00:00"'
    ' -T "${NextMonth}/${NDNextMonth}/${Year} 23:59"'
    ' -h -n > PythonReport1.txt',
    env=env, shell=True)

注意${Year} 的任何一个实例之后都没有右双引号,因为字符串没有在那里结束。

顺便说一下,大括号在这里并不是特别有用;您不妨使用$Year 等。但是,如果您的任务只是将这些值从 Python 传递到 shell,则只需使用您熟悉的任何 Python 字符串插值机制即可;

subprocess.call('forecast -J *JobABC_* -M ALL'
    ' -F "{0}/01/{2} 00:00"'
    ' -T "{0}/{1}/{2} 23:59"'
    ' -h -n > PythonReport1.txt'.format(NextMonth, NDNextMonth, Year),
    shell=True)

更好的解决方案是完全避免使用 shell。此外,可能使用check_call 而不是普通的旧call。进一步看Running Bash commands in Python.

import glob
with open('PythonReport1.txt', 'w') as handle:
    subprocess.check_call(['forecast', '-J'] + glob.glob('*JobABC_*') +
        ['-M', 'ALL', '-F', "{0}/01/{1} 00:00".format(NextMonth, Year),
         '-T', "{0}/{1}/{2} 23:59".format(NextMonth, NDNextMonth, Year),
         '-h', '-n'], stdout=handle)

【讨论】:

  • 感谢您的回答,您的解决方案看起来好多了。但是 glob 函数对我不起作用。这样就可以了:使用 open('PythonReport1.txt', 'w') 作为句柄: subprocess.check_call(['forecast', '-J', 'JobABC_' '-M', 'ALL', '-F', "{0}/01/{1} 00:00".format(NextMonth, Year), '-T', "{0}/{1}/{2} 23: 59".format(NextMonth, NDNextMonth, Year), '-h', '-n'], stdout=handle)
  • 如您所见,cmets 中的代码确实不起作用。使用shell=True,您的shell 将扩展通配符;没有shell=True,你也必须自己做。如果您不希望通配符匹配任何文件,则应从 shell 引用它(或使用 shell=True);但是你当然可以将它作为 Python 中的逐字字符串传递。
猜你喜欢
  • 2019-06-22
  • 2015-05-16
  • 2011-12-19
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 2016-05-28
相关资源
最近更新 更多