LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-26-2007, 12:56 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 206Reputation: 206Reputation: 206
python, os.system() function. howto use python variables?


i would like to do something like this in python:

Code:
myCoolVariable="some_string"
os.system("echo myCoolVariable")
however, the system function views myCoolVariable as a string literal, not as a python variable. What do I do to get around this?

Thank you,
Jameson
 
Old 07-26-2007, 01:35 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
It needs to be one of these forms:
Code:
>>> os.system('echo "asdg"')
asdg
0
>>> os.system("echo 'asdgwere'")
asdgwere
0
The outer quotes define the string to be passed to the shell, and the inner quotes define what gets passed to echo.
 
Old 07-26-2007, 01:56 PM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 206Reputation: 206Reputation: 206
That is just printing out the string literals you are passing to echo - i am looking to resolve the python varaible myCoolVariable before the execution of echo in the sysem() call.

For example, I would like:

Code:
$ python
>>>hamburger="potato"
>>>import os
>>>os.system("echo 'hamburger'")
to somehow produce

Code:
potato
0
in this case i could just write "potato" directly, but things get stickier when i want to say

Code:
hamburger=os.system('echo $MY_SHELL_VAR | sed "s/longsed/replacement/g"')
, or something of that flavour

Last edited by jhwilliams; 07-26-2007 at 02:01 PM.
 
Old 07-26-2007, 04:14 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Bingo!!!

Code:
>>> string="python is cool"
>>> cmd="echo "+string
>>> os.system(cmd)
python is cool
0
>>>
 
Old 07-26-2007, 05:18 PM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 206Reputation: 206Reputation: 206
Thanks pixellany!
 
Old 07-28-2007, 01:56 AM   #6
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Or do string interpolation:

Code:
bash$ python 
Python 2.4.3 (#1, May 18 2006, 07:40:45) 
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import system
>>> food = 'spam'
>>> system('echo %(food)s' % locals())
spam
0
>>>
- Paddy.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create variables in python darkleaf Programming 7 05-28-2007 04:29 PM
Source Bash variables into Python environ. hepburnenthorpe Programming 1 05-07-2007 10:57 AM
Python waiting function... sort of? billyseth Programming 1 04-25-2007 05:36 PM
bizarre python behavior - simple cgi variables SerfurJ Programming 2 04-14-2004 02:58 PM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM


All times are GMT -5. The time now is 05:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration