Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Python - store output of command to a variable

Programming


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-17-2017   -   Original Discussion by atanubanerji
atanubanerji's Unix or Linux Image
atanubanerji atanubanerji is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 19 November 2017, 5:39 AM EST
Location: Bangalore, India
Posts: 43
Thanks: 7
Thanked 2 Times in 2 Posts
Python - store output of command to a variable

I am trying to store output of python command in variable. Could you please help how I can do that ?

For example I am executing the following command - "CentOS" in server_desc
The output would be True or False
I would like to store the output in a variable say outPut and use condition something like -


Code:
if outPut == "True":
    do_something
else:
     do_otherthing

I just started learning python - please help/guide me.

Last edited by rbatte1; 11-17-2017 at 10:46 AM.. Reason: Changed HTML to CODE or ICODE tags
Sponsored Links
    #2  
Old Unix and Linux 11-17-2017   -   Original Discussion by atanubanerji
rbatte1's Unix or Linux Image
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 22 June 2018, 12:31 PM EDT
Location: Lancashire, UK
Posts: 3,562
Thanks: 1,571
Thanked 701 Times in 628 Posts
Hello atanubanerji,

I have a few to questions pose in response first:-
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • Beyond Python, what are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far? Please share your code so far, wrapped in CODE tags. That way we can see how you are using it and make suggestions.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.





Thanks, in advance,
Robin
Sponsored Links
    #3  
Old Unix and Linux 11-17-2017   -   Original Discussion by atanubanerji
atanubanerji's Unix or Linux Image
atanubanerji atanubanerji is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 19 November 2017, 5:39 AM EST
Location: Bangalore, India
Posts: 43
Thanks: 7
Thanked 2 Times in 2 Posts
Thank you very much Robin for your help.

I have to execute some action if the server is CentOS.

The server_description variable may or may not store CentOS key word among few other words. So I was thinking, if I could execute
"CentOS in server_description which would return either "True" or "False". If it is True, the python would perform some action. I have been advised to store the True or False value in another variable.

My problem is I am unable to store True or False in other variable, say, my_Check.

If I could do that, probably I could use the following -


Code:
if my_check == "True":
    do_something
else:
     do_otherthing

Could you please advice, how I can store a command's output, here, "CentOS in server_description in a variable (or any other python level command later) and use the value of the variable whenever required.
e.g.


Code:
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check == "True":
    print("This is CentOS")
else:
    print("This is NOT CentOS")

The output I am getting is "This is NOT CentOS"
CentOS 7.4 is OS version and Python 3.4.5
I could do it using shell, but I have been asked to use Python only, as the action to be performed based on the condition, if True or False, is already written in Python.
Please don't consider my objective is to print either CentOS or NOT CentOS, rather some other successfully written python code that I will be pasting in if or else section.

While testing, I found this, but not sure why it is not working...


Code:
>>> server_description = "CentOS"
>>> my_Check = "centos" in server_description
>>> my_Check
False
>>> my_Check = "CentOS" in server_description
>>> my_Check
True


Last edited by rbatte1; 11-17-2017 at 10:48 AM.. Reason: Converted HTML to CODE or ICODE tags and corrected some spelling
    #4  
Old Unix and Linux 11-17-2017   -   Original Discussion by atanubanerji
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 22 June 2018, 8:01 PM EDT
Posts: 352
Thanks: 16
Thanked 99 Times in 93 Posts
Try this:


Code:
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check == True:
    print("This is CentOS")
else:
    print("This is NOT CentOS")

Don't quote the "True". By quoting the "True" you were comparing a Boolean with a string. But the truth is you don't even need that:


Code:
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check:
    print("This is CentOS")
else:
    print("This is NOT CentOS")

Or even:


Code:
server_description = "This is xyz abc of mno CentOS"
if "CentOS" in server_description:
    print("This is CentOS")
else:
    print("This is NOT CentOS")

As for this:


Code:
>>> server_description = "CentOS"
>>> my_Check = "centos" in server_description
>>> my_Check
False
>>> my_Check = "CentOS" in server_description
>>> my_Check
True

Python is case-sensitive. "centos" is not "CentOS". If you want a case-insensitive string comparison, try:


Code:
"centos".lower() in server_description.lower()

There may be other ways.

Andrew
The Following User Says Thank You to apmcd47 For This Useful Post:
atanubanerji (11-19-2017)
Sponsored Links
    #5  
Old Unix and Linux 11-19-2017   -   Original Discussion by atanubanerji
atanubanerji's Unix or Linux Image
atanubanerji atanubanerji is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 19 November 2017, 5:39 AM EST
Location: Bangalore, India
Posts: 43
Thanks: 7
Thanked 2 Times in 2 Posts
Thank you very much Andrew !! Appreciate your advice.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to store output to a variable samk Shell Programming and Scripting 2 08-28-2012 11:44 PM
store sqlplus output in variable murtymvvs Shell Programming and Scripting 1 12-04-2010 12:46 PM
store the output of "find" command in a variable? punitpa Shell Programming and Scripting 4 08-04-2009 07:21 AM
How to store the sql query's output in a variable venkatesh_sasi Shell Programming and Scripting 4 01-18-2008 12:03 AM
To store the output in a variable Sudhakar333 Shell Programming and Scripting 2 07-10-2007 08:45 AM



All times are GMT -4. The time now is 08:33 PM.