Sponsored Content
Top Forums Programming Python - store output of command to a variable Post 303007501 by atanubanerji on Friday 17th of November 2017 07:21:07 AM
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
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to store output in variable when put in background

Hi, How do I store following command output: export RESULT=`date` & It works when I do : export RESULT=`date` But what I need is when command put it background, I also need that output going to RESULT variable. Is there any way ? Thanks Sanjay (1 Reply)
Discussion started by: sanjay92
1 Replies

2. Shell Programming and Scripting

To store the output in a variable

Hi, I am getting the following error while executing the script. Please can someone throw some light where is the problem. Many thanks. ./check: temp: not found The directory related to SEP instance 4 does not exist. The script is as follows. SEP_APP="/scp/sepx/app... (2 Replies)
Discussion started by: Sudhakar333
2 Replies

3. Shell Programming and Scripting

How to store the sql query's output in a variable

Hi, My requirement is : We are calling an sql statement from a UNIX session, and fetching data into some variables from a table .. now we are unable to access these variables from outside the SQL part. Please let me know how can I achieve this. Can you please share a code snippet which... (4 Replies)
Discussion started by: venkatesh_sasi
4 Replies

4. Shell Programming and Scripting

ksh: How to store each output line into a different variable?

Example output: /tmp/generatelines.sh line1 line2 line3 line4 I want each output line assigned to its own variable, ie: "line1" --> $a "line2" --> $b "line3" --> $c "line4" --> $d Is this possible without writing to a temporary file? Thanks (4 Replies)
Discussion started by: ksheller
4 Replies

5. Shell Programming and Scripting

store the output of "find" command in a variable?

I intend to find the path/full location of a file(filename given by user thru "read filenme") using "find" or any other command and then store it's output in a variable for some other processing. But struggling to put all things together (i.e finding the fully qualified location of that file and... (4 Replies)
Discussion started by: punitpa
4 Replies

6. Shell Programming and Scripting

remove column and store output to a variable

Hello guys I need to run a script to remove the last column of different comma separated files. The problem is that the number of columns of my files will be different and I won't know that number every time i run my script. Is there any command I can use to remove the last column without... (7 Replies)
Discussion started by: loperam
7 Replies

7. Shell Programming and Scripting

date output store in variable problem

When I run following command date Output1 => Thu Sep 9 03:26:52 IST 2010 When I store in a varibale as a=`date` echo $a output2 => Thu Sep 9 03:27:02 IST 2010 The differnece is, it is trimming the space when I am storing the output in varibale. Output1 = Thu Sep 9 03:26:52 IST 2010... (2 Replies)
Discussion started by: pravincpatil
2 Replies

8. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

9. Shell Programming and Scripting

store sqlplus output in variable

hi how can i store sqlplus output to a variable in sh script (not bash) Thanks MM (1 Reply)
Discussion started by: murtymvvs
1 Replies

10. Shell Programming and Scripting

how to store output to a variable

I need some help: 1) I have a out put from a shell script, the out put looks like this: Attempting privilege escalation using sudo ... List backups for CLTST: Start date Status Ret. Class Label -------------------- ------------ ------------ ... (2 Replies)
Discussion started by: samk
2 Replies
LAZYGAL.CONF(5) 					   File formats and conventions 					   LAZYGAL.CONF(5)

NAME
lazygal.conf - Configuration file for lazygal, a static web gallery generator. DESCRIPTION
The configuration file is an INI like file which configures lazygal. The format looks like this: [sectionname] variable = value othervariable = othervalue [othersection] foo = bar Boolean values can be conveniently set in the following ways: o For True: 1, yes, true, and on. o For False: 0, no, false, and off. Please refer to the python ConfigParser documentation[1] for more information on the file format. RUNTIME SECTION
The runtime defines the runtime parameters. quiet Boolean. Same as --quiet in lazygal(1) if True. (default is False). debug Boolean. Same as --debug in lazygal(1) if True (default is False). check-all-dirs Boolean. Same as --check-all-dirs in lazygal(1) if True. (default is False). GLOBAL SECTION
The global defines the global parameters. Those parameters apply to all the sub-galleries. destdir Same as --output-directory=DEST_DIR in lazygal(1) (default is current directory). clean-destination Boolean. Same as --clean-destination in lazygal(1) if True. dir-flattening-depth Same as --dir-flattening-depth=LEVEL in lazygal(1). puburl Same as --puburl=PUB_URL in lazygal(1). theme Same as --theme=THEME in lazygal(1). WEBGAL SECTION
The webgal defines the parameters for a web-gallery. default-style Same as --default-style=DEFAULT_STYLE in lazygal(1). webalbumpic-bg Same as --webalbum-pic-bg=WEBALBUMPIC_BG in lazygal(1). webalbumpic-type Same as --webalbum-pic-type=WEBALBUMPIC_BG in lazygal(1). If you set this to 'tidy' you may also consider setting webalbumpic-size (see below) to something smaller than the default 200x150. webalbumpic-size Size of picture mash-up representing galleries, eg. 200x150. image-size Same as --image-size=IMAGE_SIZE in lazygal(1). thumbnail-size Same as --thumbnail-size=THUMBNAIL_SIZE in lazygal(1). thumbs-per-page Same as --thumbs-per-page=THUMBS_PER_PAGE in lazygal(1). sort-medias Same as --pic-sort-by=ORDER in lazygal(1). sort-subgals Same as --subgal-sort-by=ORDER in lazygal(1). original Boolean. Same as --original in lazygal(1) if True (default is False). original-baseurl Same as --orig-base=RELATIVE_PATH in lazygal(1). original-symlink Boolean. Same as --orig-symlink in lazygal(1) if True (default is False). dirzip Same as --make-dir-zip in lazygal(1) if True (default is False). jpeg-quality Same as --quality=QUALITY in lazygal(1). jpeg-optimize Boolean. Run an extra optimization pass for each generated thumbnail if True, the default. jpeg-progressive Generate progressive JPEG images if True, the default. publish-metadata Publish image metadata if True, the default: copy original image metadata in reduced picture, and include some information in the image page. TEMPLATE-VARS SECTION The template-vars defines the custom template variables. The variables and their value are listed in this section. Example 1. $footer in the default template For instance, $footer is a template variable in the default template. Its value can be defined with this configuration file: [template-vars] footer = <p>All pics are copyright 2011 me</p> SEE ALSO
lazygal(1). AUTHOR
This manual page was written for the Debian system (but may be used by others). Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 any later version published by the Free Software Foundation. On Debian systems, the complete text of the GNU General Public License can be found in /usr/share/common-licenses/GPL. COPYRIGHT
Copyright (C) 2011 NOTES
1. the python ConfigParser documentation http://docs.python.org/library/configparser.html lazygal August 2011 LAZYGAL.CONF(5)
All times are GMT -4. The time now is 07:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy