【问题标题】:running complex commands of bash on python在 python 上运行复杂的 bash 命令
【发布时间】:2020-01-10 08:27:03
【问题描述】:

无论如何要把脚本集成到操作系统中?

例如,我有一个名为 getInfo 的文件

getInfo 是这个文件:

KEY=$(grep -i -e "^$1:" username.sql |sed 's/.*://')
grep -e "^$KEY" info.txt | cut -d ':' -f 2

细节并不重要,但基本上它只是获取一些用户名的信息。 bash 中的示例

$ ./getInfo username1
Hello

在python中会是

import os
os.system('./getInfo username1')

我会在 bash 上运行这个 python 文件

$ python this.py
'KEY' is not recognized as an internal or external command, operable program or batch file.

有没有办法在 python 中完成这个?

【问题讨论】:

  • getInfo 有合适的 shebang 行来用 bash 运行它吗?

标签: python bash python-2.7


【解决方案1】:

您的 getInfo.sh 应该有 #!/bin/sh 作为第一行。

推荐使用subprocess模块来执行bash脚本/命令。

import subprocess
subprocess.call("./getInfo.sh", shell=True)

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 2023-01-20
    • 1970-01-01
    • 2012-08-24
    • 2013-11-29
    • 2011-05-14
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多