Use the subprocess module instead:
import subprocess
output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'", shell=True)
Edit: this is new in Python 2.7. In earlier versions this should work (with the command rewritten as shown below):
import subprocess
output = subprocess.Popen(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'], stdout=subprocess.PIPE).communicate()[0]
As a side note, you can rewrite
cat syscall_list.txt | grep f89e7000
To
grep f89e7000 syscall_list.txt
And you can even replace the entire statement with a single awk script:
awk '/f89e7000/ {print $2}' syscall_list.txt
Leading to:
import subprocess
output = subprocess.check_output(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'])
Answer from Rob Wouters on Stack OverflowUse the subprocess module instead:
import subprocess
output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'", shell=True)
Edit: this is new in Python 2.7. In earlier versions this should work (with the command rewritten as shown below):
import subprocess
output = subprocess.Popen(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'], stdout=subprocess.PIPE).communicate()[0]
As a side note, you can rewrite
cat syscall_list.txt | grep f89e7000
To
grep f89e7000 syscall_list.txt
And you can even replace the entire statement with a single awk script:
awk '/f89e7000/ {print $2}' syscall_list.txt
Leading to:
import subprocess
output = subprocess.check_output(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'])
In python 3 you can use
import subprocess as sp
output = sp.getoutput('whoami --version')
print (output)
bash - Python output to a shell variable - Unix & Linux Stack Exchange
http redirect - Any way to assign terminal output to variable with python? - Stack Overflow
redirecting python output to bash variables
Use python to execute shell command and redirect the output to a variable - Stack Overflow
os.system returns a return value indicating the success or failure of the command. It does not return the output from stdout or stderr. To grab the output from stdout (or stderr), use subprocess.Popen.
import subprocess
proc=subprocess.Popen('echo "to stdout"', shell=True, stdout=subprocess.PIPE, )
output=proc.communicate()[0]
print output
os.system returns the exit code of the executed command, not its output. To do this you would need to use either commands.getoutput (deprecated) or subprocess.Popen:
from subprocess import Popen, PIPE
stdout = Popen('your command here', shell=True, stdout=PIPE).stdout
output = stdout.read()
I have a python script that I want to run in a .sh file
the python script contains two print statements one of them is a number and another is a link
i would like to further use them in my bash as two separate variables.
!/bin/sh
outputString=$(python3 draft.py) echo $outputString < -- this prints my number and a link that I want to assign to two variables to further use in my bash.
Any help is welcome
Thanks
EDIT:
I found the set command that seems to split it into variables and can call them with $1 and $2.
Thanks for the help guys.
outputString=$(python3 draft.py)
set $outputString
wget $2
gzip $1.gz -d
I currently have this function which I can pass IPs into and it will display what I want. The issue is I am trying to take the output and perform a conditional against it. I feel like there is something simple I am missing here, but I can't seem to figure it out. Note: That print statement works, I just cannot get it into a variable (Always ends up empty it seems)
def linuxConnect(ip):
client.connect(hostname=ip, username=linuxUname, password=linuxPwd)
stdin, stdout, stderr = client.exec_command('nginx -v')
print(stdout.read().decode())
err = stderr.read().decode()
if err:
print(err)
client.close()
return