[Wolves] Another python question, running commands with variables as arguments.

Simon Burke simonb at fatsportsman.eu
Thu Aug 18 10:19:25 UTC 2011


On Thu, Aug 18, 2011 at 10:47 AM, Simon Burke <simonb at fatsportsman.eu> wrote:
> On Wed, Aug 17, 2011 at 8:12 PM, Alex Willmer <alex at moreati.org.uk> wrote:
>> On 17 August 2011 12:56, Simon Burke <simonb at fatsportsman.eu> wrote:
>>> All I want to do is run a command, but arguments are stored in variables.
>>>
>>> This is where Im struggling.
>>>
>>> I have been using subprocess.Popen to run commands with little
>>> problem, but as soon as I want to get variables involved it all falls
>>> to pieces.
>>> My understanding is that I need to possibly use
>>> subprocess.Popen,communicate but I cant find a clear example or doc to
>>> detail how I am supposed to do this.
>>>
>>> Can someone help or point me in the right direction?
>>
>> Simon,
>> It depends what you mean by variables and what you want to achieve.
>> Are you talking about Python variables (e.g. sys.argv), or environment
>> variables (i.e. $PWD, $HOME etc)?
>>
>> You could use a variable as one of the arguments doing something like
>> the following:
>> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
>> [GCC 4.4.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import subprocess
>>>>> path = '/'
>>>>> pipe = subprocess.Popen(['/bin/ls', path], stdout=subprocess.PIPE)
>>>>> pipe.communicate()
>> ('bin\nboot\ncdrom\ndev\netc\nhome\ninitrd.img\nlib\nlost+found\nmedia\nmnt\nopt\nproc\nroot\nsbin\nselinux\nsrv\nsys\ntmp\nusr\nvar\nvmlinuz\n',
>> None)
>>
>> If you paste the code you have, and ask more specific question(s) we
>> should be able to help you much better. In general, vague questions
>> don't help us or you.
>>
>> Regards, Alex
>
> An example of the code I'm trying to use is: (the code currently just
> prints out the command).
>
>                        for filesys in bp1_fs:
>                                print "mount %s:/vol/%s  -t nfs -o %s"
> % (nas1, filesys, moptions)
>                        for ipaddr in bp1_ip:
>                                print "ip addr add %s dev %s" % (ipaddr, int)
>                        print "su - %s -c startdb" % (bp1_usr)
>                        for inst in bp1_inst:
>                                print "su - %s -c startsap %s" % (bp1_usr, inst)
>
>
> So far I have been pointed into putting the commands into an array and
> run through it with Popen.communicate, but its the how I'm struggling
> with.
>

Ok, I found a resolution but not a permanent one. If I get the
commands from the above and flatten them out into an array I can then
run through them with os.popen. I know os.popen is depreciated, but
until I can get subprocess.popen to do the same it will do.

--
Regards,
Simon.



More information about the Wolves mailing list