在 Python 上运行复杂的 Bash 命令



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

例如,我有一个名为getInfo

getInfo 是这个文件:

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

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

$ ./getInfo username1
Hello

在蟒蛇中,这将是

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.sh 应该将#!/bin/sh作为第一行。

建议使用subprocess模块来执行 bash 脚本/命令。

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

最新更新