12

What i'd like to have is a mechanism that all commands i enter on a Bash-Terminal are wrapped by a Python-script. The Python-script executes the entered command, but it adds some additional magic (for example setting "dynamic" environment variables). Is that possible somehow?

I'm running Ubuntu and Debian Squeezy.

Additional explanation:

I have a property-file which changes dynamically (some scripts do alter it at any time). I need the properties from that file as environment variables in all my shell scripts. Of course i could parse the property-file somehow from shell, but i prefer using an object-oriented style for that (especially for writing), as it can be done with Python (and ConfigObject).

Therefore i want to wrap all my scripts with that Python script (without having to modify the scripts themselves) which handles these properties down to all Shell-scripts. This is my current use case, but i can imagine that i'll find additional cases to which i can extend my wrapper later on.

1
  • Could you please elaborate more what you want to do? How are you going to wrap shell commands and how are you going to execute them? It can done in two (or maybe more ways): a) handle the command as a string, add something to that string and run that string in bash; b) get the command, execute the bash using subprocess or something else and pass the command (preceded by some another command). Commented Dec 12, 2010 at 10:21

4 Answers 4

16

The perfect way to wrap every command that is typed into a Bash Shell is to change the variable PROMPT_COMMAND inside the .bashrc. For example, if I want to do some Python stuff before every command, liked asked in my question:

.bashrc:

# ...
PROMPT_COMMAND="python mycoolscript.py; $PROMPT_COMMAND;"
export $PROMPT_COMMAND
# ...

now before every command the script mycoolscript.py is run.

Sign up to request clarification or add additional context in comments.

Comments

1

Use Bash's DEBUG trap. Let me know if you need me to elaborate.

Edit:

Here's a simple example of the kinds of things you might be able to do:

$ cat prefix.py
#!/usr/bin/env python
print "export prop1=foobar"
print "export prop2=bazinga"
$ cat propscript
#!/bin/bash
echo $prop1
echo $prop2
$ trap 'eval "$(prefix.py)"' DEBUG
$ ./propscript
foobar
bazinga

You should be aware of the security risks of using eval.

4 Comments

I read some stuff about the debug trap, but i could not find any way how to use it for my "problem"
@ifischer: Your python script can influence the environment of a child shell, but it can't influence the environment of the current shell. You will need to do your processing in Bash using the DEBUG trap (possibly with Python doing some processing and outputting the result as a string for Bash to apply in some way) or use a wrapper as you have done in your answer. If you post more specific details on what you're trying to accomplish, I can be more help or direct you to a different method to use.
It's ok that the variables are not influenced in the current shell, as long as i run every commando with my x-script. So i'm pretty satisfied with my solution, except that i have to type x before every command (but this also has kind of advantages if i want to run scripts without this mechanisms). I gave some additional explanations about my use case in my question. Let me know if it's clear enough now.
hmm, that way i would have to change all of my scripts. But this is what i'm trying to avoid - i need just a "wrapper". Anyway, thanks for your thoughts
0

I don't know of anything but two things that might help you follow

  1. http://sourceforge.net/projects/pyshint/
  2. The iPython shell has some functionality to execute shell commands in the iterpreter.

1 Comment

Yeah i know ipython but i also want to py-ify my standard shell ;)
-3

There is no direct way you can do it . But you can make a python script to emulate a bash terminal and you can use the beautiful "Subprocess" module in python to execute commnands the way you like

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.