4

Without using the new 2.6 subprocess module, how can I get either os.popen or os.system to execute my commands using the tcsh instead of bash? I need to source some scripts which are written in tcsh before executing some other commands and I need to do this within python2.4.

EDIT Thanks for answers using 'tcsh -c', but I'd like to avoid this because I have to do escape madness. The string will be interpreted by bash and then interpreted by tcsh. I'll have to do something like:

os.system("tcsh -c '"+re.compile("'").sub(r"""'"'"'""",my_cmd)+"'")

Can't I just tell python to open up a 'tcsh' sub-process instead of a 'bash' subprocess? Is that possible?

P.S. I realize that bash is the cat's meow, but I'm working in a corporate environment and I'm going to choose to not fight a tcsh vs bash battle -- bigger fish to fry.

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
  • what do you mean by "source"? –  Feb 10 '09 at 20:23
  • If you execute a command like 'source env_mod.csh' the script will modify your current shell. If you do 'tcsh -f env_mod.csh' the shell script will create a child shell that inherits the environment variables of your current shell and execute without modifying the parent shell. – Ross Rogers Feb 10 '09 at 21:51
  • Example includes your ~/.bashrc file which gets 'sourced' when you start a new bash shell. – Ross Rogers Feb 10 '09 at 21:53
  • i know that, i just wasn't sure about _where_ you actually wanted to source something -- from within os.system() or from a shell script you call with os.system()... –  Feb 11 '09 at 00:18
  • There are many Electronic Design Automation CAD tools (e.g. Specman, VManager, VCS, NCSim) which provide an env.csh which you source to setup your PATH, license server, env pointers to different pieces. Basically I want to source those env setup files and then execute a command. – Ross Rogers Feb 11 '09 at 15:20
  • e.g. os.system('source $SPECMAN_HOME/env.csh -64bit && specman -c "load foo.e; write stubs -vcssv"')
    The problem is that os.system executes bash and I need tcsh because that's what everyone uses at my company.
    – Ross Rogers Feb 11 '09 at 15:22

3 Answers3

11

Just prefix the shell as part of your command. I don't have tcsh installed but with zsh:

>>> os.system ("zsh -c 'echo $0'")
zsh
0
anthony
  • 40,424
  • 5
  • 55
  • 128
5

How about:

>>> os.system("tcsh your_own_script")

Or just write the script and add

#!/bin/tcsh

at the beginning of the file and let the OS take care of that.

Jay
  • 41,768
  • 14
  • 66
  • 83
Torsten Marek
  • 83,780
  • 21
  • 91
  • 98
-1

Just set the shell to use to be tcsh:

>>> os.environ['SHELL'] = 'tcsh'
>>> os.environ['SHELL']
'tcsh'
>>> os.system("echo $SHELL")
tcsh
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127