views:

204

answers:

1

NOTE: This is not the same question as Python: Persistent shell variables in subprocess, as that question is actually about environment variables, not shell variables.

I'm trying to automate a basic benchmark that I'm doing in csh using the shell built-in time command. You can tweak the output of time by setting the variable of the same name in the shell like follows:

set time=(1000000000 "%U %S %E %P %W %X %D %K %M %F %R %I %O %r %s %k %w %c")

However, this is of course not an environment variable, so you can't pass it in the env parameter in the subprocess.Popen constructor (I tried that first...). You can hack it together by abusing (imo, anyhow) the cmd argument to do that, and then your actual work (shortened to display nicely):

time_cmd = 'set time=(1000000000 "%U %S %E %P %c"); time ./some_func'
p = subprocess.Popen(cmd, executable="/bin/csh", shell=True, ...)

and that works as you would expect, but it seems not really the right way to go (and can lead to some pretty horrible cmd construction when you want to tweak a bunch of different shell behaviours).

Is there any other way to set shell variables in subprocess other than this kludge?

+1  A: 

You have a limited number of options to communicate to a child process (invocation arguments, environment, and streams). Since setting a shell variable is in a more general sense modifying a child process's internal state, what you call a "kludge" is actually quite reasonable. I don't know of any csh variants that have a special way to load shell variables.

However, you could neaten it up by writing all your initialisation commands to the shell via a pipe, or even writing them to a file and having them read by csh's source builtin.

p00ya
This is probably a good way to look at it (unfortunately). While most shells seem to have shell variables, their behaviour undoubtably isn't governed by POSIX in the way that environment variables are. Unfortunately I haven't found the proper magic sauce to communicate with an actual *shell* via pipe. This seems more like a job for pyexpect or custom tty munging, both of which are less appealing than my existing solution. :-)
Nick Bastin