Unable to store "python --version" to a shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to store "python --version" to a shell variable
# 1  
Old 05-26-2014
Unable to store "python --version" to a shell variable

Hi All,
I need to get the version of python installed and store it in a variable for later use. Whereas it is printing on the console instead of storing to variable. I am able to store output of ls command in a variable. Please check the below code :
Code:
root@myhost:/volumes/srini# cat python_version.sh 
python_version=python --version
echo "****"
echo $python_version
echo "****"
ls_output=ls -l python*
echo "****"
echo $ls_output
echo "****"

Please check the output of the code :
Code:
root@myhost:/volumes/srini# ./python_version.sh 
Python 2.6.8
****

****
****
-rwxr-xr-x 1 root root 147 May 26 09:35 python_version.sh
****

Please note, its able to store output of ls command in a variable, but not of python --version. Its directly displaying in console.

Regards,
Srinivasan

Last edited by srinivasan.neel; 05-26-2014 at 10:35 AM.. Reason: sorry for the typo, even after changing that also its not getting stored in variable
# 2  
Old 05-26-2014
Code:
echo $python_verison

Hehe typos!
# 3  
Old 05-26-2014
Ok what shell are you using, because so far not knowing I cant reply because of its behaviour: In shells I use (sh ksh...) your variable affectation would not work, and so you would not have any output for your ls part either

this is what I would have written:
Code:
python_version=$(python --version)
echo "****"
echo $python_version
echo "****"
ls_output=$(ls -l python*)
echo "****"
echo $ls_output
echo "****"


Last edited by vbe; 05-26-2014 at 11:47 AM.. Reason: typos
# 4  
Old 05-26-2014
If you're using bash/sh or similar shells this will capture the python version number in a variable.

Code:
xx=$(python --version 2>&1)

The output is being sending to stderr, which is why you need the "2>&1".
This User Gave Thanks to cnamejj For This Post:
# 5  
Old 05-27-2014
Thanks a lot cnamejj. I am using bash shell and redirection of stderr worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh?

Please can you help me understand the significance of providing arguments under sh -s in > ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh (4 Replies)
Discussion started by: Sree10
4 Replies

2. Solaris

Netra X1 LOM: Unable to change any variable via the "set" command

I'm posting here as it didn't seem quite right in the hardware section (as it's LOM commands). My apologies if I have that wrong though :) I've finally gotten round to configuring the LOM on my Netra X1, but I can't get it to change the hostname via the "set" command: lom>show hostname... (2 Replies)
Discussion started by: Smiling Dragon
2 Replies

3. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

4. Shell Programming and Scripting

Store Host lookup in variable ("on the fly")

Hi, I'm new here. I was wondering why I can't store a host lookup in a variable. for line in $(< blacklist) do STOREIP=host $line; if ]; then $line >> blacklist2; else $line >> blacklist3; fi done Result: "ip" command not found .. so how would I store the host lookup in the... (2 Replies)
Discussion started by: sOliver
2 Replies

5. Shell Programming and Scripting

Unable to store "-e" in variable ??????

p="-e" echo $p It is not returning the value "-e" stored. Instead returns null. I am wondering how could this happen. Please help me out.I tried all possibilities like p='-e' | p="\-e". Nothing seems to work. :confused::confused: (10 Replies)
Discussion started by: shanneykar
10 Replies

6. 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

7. AIX

"pconsole" and "esaadmin" on AIX version 6

Anyone know what is the function of user "pconsole" and "esaadmin" on AIX version 6? (1 Reply)
Discussion started by: ebab3
1 Replies

8. Shell Programming and Scripting

How to store the output of "time dd if= of=" in a variable

Hi All, I need to store the real seconds of the following command in a variable. How could it be done? time $(dd if=/dev/zero of=/dev/sda1 bs=512 count=2048;sync) Thanks, Amio (12 Replies)
Discussion started by: amio
12 Replies

9. SuSE

VMDB Failure" followed by "Unable to open snapshot file"

keep getting an error when I try to revert to a snapshot: "VMDB Failure" followed by "Unable to open snapshot file" Im using vmware server 1.0.4, host OS is windows xp and guest OS is SLES. Is there anything I can do to recover the snapshot or am I in trouble!?!?! (0 Replies)
Discussion started by: s_linux
0 Replies

10. Programming

getting "mi_cmd_var_create: unable to create variable object" error msg

Hi, i am working in C in Fedora Eclipse 3.3.0 with gdb debugger. I am geting segmentation fault with an error message "mi_cmd_var_create: unable to create variable object" on debugging the program. What should I do to solve this problem? rgds, Dona_m (14 Replies)
Discussion started by: dona_m
14 Replies
Login or Register to Ask a Question