Home
Man
Search
Today's Posts
Register

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

Python - store output of command to a variable

Tags
programming

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 11-17-2017
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 11:46 AM.. Reason: Changed HTML to CODE or ICODE tags
# 2  
Old 11-17-2017
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
# 3  
Old 11-17-2017
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 11:48 AM.. Reason: Converted HTML to CODE or ICODE tags and corrected some spelling
# 4  
Old 11-17-2017
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)
# 5  
Old 11-19-2017
Thank you very much Andrew !! Appreciate your advice.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unable to store "python --version" to a shell variable srinivasan.neel Shell Programming and Scripting 4 05-27-2014 01:25 AM
how to store output to a variable samk Shell Programming and Scripting 2 08-29-2012 12:44 AM
store sqlplus output in variable murtymvvs Shell Programming and Scripting 1 12-04-2010 01:46 PM
date output store in variable problem pravincpatil Shell Programming and Scripting 2 09-16-2010 04:55 AM
remove column and store output to a variable loperam Shell Programming and Scripting 7 09-03-2010 04:12 PM
store the output of "find" command in a variable? punitpa Shell Programming and Scripting 4 08-04-2009 08:21 AM
ksh: How to store each output line into a different variable? ksheller Shell Programming and Scripting 4 11-06-2008 08:01 AM
How to store the sql query's output in a variable venkatesh_sasi Shell Programming and Scripting 4 01-18-2008 01:03 AM
To store the output in a variable Sudhakar333 Shell Programming and Scripting 2 07-10-2007 09:45 AM
How to store output in variable when put in background sanjay92 UNIX for Dummies Questions & Answers 1 02-22-2005 03:41 PM


All times are GMT -4. The time now is 09:41 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password