0

I am a newbie to python and i am having issues in having the output of shell command assigned to variable.

I have a python script which runs shell script cmd line and output something like below

Name: test Version: i22349

I want to use this output as input for next operation. How can i assign a variable with the outputted value. Could someone help me with this.

Here is the code which i am using

import os
deploymentName = 'testdeployment'
myCmd = 'list dep ' + deploymentName + ' | egrep "^Version|Deck Name"'
print ("Fetching the Deck details - Running Command - " + myCmd)
os.system(myCmd)

Currently it returns output as ,

Name:       test
Version:    i22349

How do i get the value test and i22349 assigned to 2 variables inside the python code ?

sangs8788
  • 1
  • 2
  • Possible duplicate of [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – Underoos May 15 '19 at 10:41
  • @SukumarRdjf The link you shared converts entire output to string and display result. Mine is partly output which i need to extract out of the shell output and assign that to a variable. In my case, Version: i22349, i want the value i22349 to be assigned to a variable. How do i extract the value out of one single output ? It might be super easy but i am totally new to this language, any kind of help would be really helpful. – sangs8788 May 15 '19 at 14:19
  • Capture the whole output, split the lines, get the index of `:` and slice the line from there too its end. – Underoos May 15 '19 at 14:35

0 Answers0