简体   繁体   中英

running complex commands of bash on python

Anyway to integrate scripts into os ?

For example I have a file called getInfo

getInfo is this file:

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

The specifics isn't important but basically it just gets the info of some username. Example in bash

$ ./getInfo username1
Hello

In python it would be

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

And I'll run this python file on bash

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

Is there a way to accomplish this in python?

Your getInfo.sh should have #!/bin/sh as the first line.

It is recommended to use subprocess module for executing bash scripts/commands.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM