使用Python脚本包装在Bash-Shell中输入的所有命令

时间:2022-02-24 16:24:10

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?

我想要的是一种机制,我在Bash-Terminal上输入的所有命令都由Python脚本包装。 Python脚本执行输入的命令,但它增加了一些额外的魔法(例如设置“动态”环境变量)。这有可能吗?

I'm running Ubuntu and Debian Squeezy.

我正在运行Ubuntu和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).

我有一个动态更改的属性文件(某些脚本会随时更改它)。我需要该文件中的属性作为所有shell脚本中的环境变量。当然我可以从shell以某种方式解析属性文件,但我更喜欢使用面向对象的样式(尤其是写入),因为它可以用Python(和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.

因此,我想用Python脚本包装所有脚本(无需修改脚本本身),这些脚本处理这些属性到所有Shell脚本。这是我目前的用例,但我可以想象,我会发现其他情况,我可以在以后扩展我的包装。

4 个解决方案

#1


12  

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:

将每个键入Bash Shell的命令包装的完美方法是更改​​.bashrc中的变量PROMPT_COMMAND。例如,如果我想在每个命令之前做一些Python的东西,喜欢在我的问题中询问:

.bashrc:

.bashrc中:

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

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

现在在每个命令之前运行脚本mycoolscript.py。

#2


1  

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

使用Bash的DEBUG陷阱。如果您需要我详细说明,请告诉我。

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.

您应该了解使用eval的安全风险。

#3


0  

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

除了两件可能对你有所帮助的事情我什么都不知道

  1. http://sourceforge.net/projects/pyshint/
  2. http://sourceforge.net/projects/pyshint/
  3. The iPython shell has some functionality to execute shell commands in the iterpreter.
  4. iPython shell具有一些在iterpreter中执行shell命令的功能。

#4


-2  

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

没有直接的方法可以做到。但你可以制作一个python脚本来模拟一个bash终端,你可以使用python中漂亮的“Subprocess”模块以你喜欢的方式执行commnands

#1


12  

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:

将每个键入Bash Shell的命令包装的完美方法是更改​​.bashrc中的变量PROMPT_COMMAND。例如,如果我想在每个命令之前做一些Python的东西,喜欢在我的问题中询问:

.bashrc:

.bashrc中:

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

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

现在在每个命令之前运行脚本mycoolscript.py。

#2


1  

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

使用Bash的DEBUG陷阱。如果您需要我详细说明,请告诉我。

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.

您应该了解使用eval的安全风险。

#3


0  

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

除了两件可能对你有所帮助的事情我什么都不知道

  1. http://sourceforge.net/projects/pyshint/
  2. http://sourceforge.net/projects/pyshint/
  3. The iPython shell has some functionality to execute shell commands in the iterpreter.
  4. iPython shell具有一些在iterpreter中执行shell命令的功能。

#4


-2  

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

没有直接的方法可以做到。但你可以制作一个python脚本来模拟一个bash终端,你可以使用python中漂亮的“Subprocess”模块以你喜欢的方式执行commnands