Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap all commands entered within a Bash-Shell with a Python script

Tags:

python

linux

bash

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.

like image 682
Wolkenarchitekt Avatar asked Dec 11 '10 19:12

Wolkenarchitekt


People also ask

How do you wrap a Python script?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.

Which commands will execute a Python script called script in bash?

The -s option to sh (and to bash ) tells the shell to execute the shell script arriving over the standard input stream. The script then starts python - , which tells Python to run whatever comes in over the standard input stream.

Can you use Python in bash?

The same goes for any Python script. Generally, a Python script will have the file extension PY. However, there's another way of writing a Python script: embedding Python codes into a bash script. Either way, you need to have the Python package installed in your system.


2 Answers

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.

like image 62
Wolkenarchitekt Avatar answered Oct 19 '22 06:10

Wolkenarchitekt


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.

like image 39
Dennis Williamson Avatar answered Oct 19 '22 05:10

Dennis Williamson