简体   繁体   English

bash用python脚本包装管道命令

[英]bash wrap a piped command with a python script

Is there a way to create a python script which wraps an entire bash command including the pipes. 有没有一种方法来创建一个python脚本,该脚本会包装包括管道在内的整个bash命令。

For example, if I have the following simple script 例如,如果我有以下简单脚本

import sys
print sys.argv

and call it like so (from bash or ipython), I get the expected outcome: 并这样称呼它(从bash或ipython),我得到了预期的结果:

[pkerp@pendari trell]$ python test.py ls
['test.py', 'ls']

If I add a pipe, however, the output of the script gets redirected to the pipe sink: 但是,如果添加管道,则脚本的输出将重定向到管道接收器:

[pkerp@pendari trell]$ python test.py ls > out.txt

And the > out.txt portion is not in sys.argv. 并且> out.txt部分不在sys.argv中。 I understand that the shell automatically process this output, but I'm curious if there's a way to force the shell to ignore it and pass it to the process being called. 我知道Shell会自动处理此输出,但是我很好奇是否有一种方法可以迫使Shell忽略它并将其传递给正在调用的进程。

The point of this is to create something like a wrapper for the shell. 这样做的目的是为外壳创建类似包装的东西。 I'd like to run the commands regularly, but keep track of the strace output for each command (including the pipes). 我想定期运行命令,但要跟踪每个命令(包括管道)的strace输出。 Ideally I'd like to keep all of the bash features, such as tab-completion and up and down arrows and history search, and then just pass the completed command through a python script which invokes a subprocess to handle it. 理想情况下,我想保留所有bash功能,例如制表符补全和上下箭头以及历史记录搜索,然后将完成的命令通过python脚本传递,该脚本调用一个子进程来处理它。

Is this possible, or would I have to write my own shell to do this? 这可能吗,或者我必须编写自己的shell来做到这一点?

Edit 编辑

It appears I'm asking the exact same thing as this question . 看来我在问与这个问题完全一样的事情。

Well, I don't quite see what you are trying to do. 好吧,我不太明白您要做什么。 The general approach would be to give the desired output destination to the script using command line options: python test.py ls --output=out.txt . 一般的方法是使用命令行选项将所需的输出目标提供给脚本: python test.py ls --output=out.txt Incidentally, strace writes to stderr. 顺便说一句,strace写入stderr。 You could capture everything using strace python test.py > out 2> err if you want to save everything... 如果要保存所有内容,可以使用strace python test.py > out 2> err捕获所有内容...

Edit: If your script writes to stderr as well you could use strace -o strace_out python test.py > script_out 2> script_err 编辑:如果您的脚本也写到stderr,则可以使用strace -o strace_out python test.py > script_out 2> script_err

Edit2: Okay, I understand better what you want. Edit2:好的,我了解您想要什么。 My suggestion is this: Write a bash helper: 我的建议是:写一个bash助手:

function process_and_evaluate()
{

  strace -o /tmp/output/strace_output "$@"

  /path/to/script.py /tmp/output/strace_output
}

Put this in a file like ~/helper.sh . 将其放在~/helper.sh类的文件中。 Then open a bash, source it using . ~/helper.sh 然后打开一个bash,使用为其提供源. ~/helper.sh . ~/helper.sh . . ~/helper.sh Now you can run it like this: process_and_evaluate ls -lA . 现在,您可以像这样运行它: process_and_evaluate ls -lA

Edit3: To capture output / error you could extend the macro like this: Edit3:要捕获输出/错误,可以像这样扩展宏:

function process_and_evaluate()
{
  out=$1
  err=$2

  shift 2

  strace -o /tmp/output/strace_output "$@" > "$out" 2> "$err"

  /path/to/script.py /tmp/output/strace_output
}

You would have to use the (less obvious ) process_and_evaluate out.txt err.txt ls -lA . 您将不得不使用(不太明显的) process_and_evaluate out.txt err.txt ls -lA This is the best that I can come up with... 这是我能想到的最好的...

The only thing you can do is pass the entire shell command as a string, then let Python pass it back to a shell for execution. 您唯一可以做的就是将整个shell命令作为字符串传递,然后让Python将其传递回shell以执行。

$ python test.py "ls > out.txt"

Inside test.py , something like test.py内部,类似

subprocess.call("strace " + sys.argv[1], shell=True, executable="/bin/bash")

to ensure the entire string is passed to the shell (and bash , specifically). 以确保将整个字符串传递到shell(特别是bash )。

At least in your simple example, you could just run the python script as an argument to echo, eg 至少在您的简单示例中,您可以将python脚本作为回显的参数运行,例如

$ echo $(python test.py ls) > test.txt
$ more test.txt
['test.py','ls']

Enclosing a command in parenthesis with a dollar sign first executes the contents then passes the output as an argument to echo. 将命令括在圆括号中并带有美元符号,将首先执行内容,然后将输出作为自变量传递给echo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM