巴什(或其他shell):用保鲜功能/脚本的所有命令 [英] Bash (or other shell): wrap all commands with function/script

查看:155
本文介绍了巴什(或其他shell):用保鲜功能/脚本的所有命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这个问题本来是来砸具体。我还是宁愿有一个bash的解决方案,但如果有一个很好的方式在另一个shell做到这一点那么这将是有益的了解以及!

This question was originally bash specific. I'd still rather have a bash solution, but if there's a good way to do this in another shell then that would be useful to know as well!

好吧,这个问题的顶层描述。我希望能够添加一个钩子来砸,这样,当用户输入,例如 $猫富|排序-n |少,这是截获并翻译成包装'猫富|排序-n |少。我见过的方式之前,运行命令和每个命令后(使用DEBUG陷阱或PROMPT_COMMAND或类似),但没有关于如何截取每个命令,并允许它被其他进程进行处理。有没有办法做到这一点?

Okay, top level description of the problem. I would like to be able to add a hook to bash such that, when a user enters, for example $cat foo | sort -n | less, this is intercepted and translated into wrapper 'cat foo | sort -n | less'. I've seen ways to run commands before and after each command (using DEBUG traps or PROMPT_COMMAND or similar), but nothing about how to intercept each command and allow it to be handled by another process. Is there a way to do this?

有关的原因,我想这样做,万一人的解释有其他办法的建议,来解决:

For an explanation of why I'd like to do this, in case people have other suggestions of ways to approach it:

脚本工具让你登录你在一个终端到日志做的一切(如,在一定程度上,确实bash的历史)。然而,他们不这样做非常好 - 脚本融合了输出输入到一个大的字符串,并获取与应用,如VI其中占据整个屏幕的困惑,历史只给您正在键入在原始命令,他们都不工作那么,如果你已经被命令进入到多个终端在同一时间。我想这样做的是捕捉更丰富的信息 - 作为一个例子,命令,它执行的时候,它完成的时候,退出状态,stdin和stdout的前几行。我也preFER某处发送给侦听守护进程可能高高兴兴地把多个终端。最简单的方式做,这是命令传递给另一个程序,可以Exec的一个shell来处理命令作为一个子进程,同时让手柄标准输入,标准输出,退出状态等,可以写一个shell来做到这一点,但你' ð失去很多已经在bash的功能,这将是烦人。

Tools like script let you log everything you do in a terminal to a log (as, to an extent, does bash history). However, they don't do it very well - script mixes input with output into one big string and gets confused with applications such as vi which take over the screen, history only gives you the raw commands being typed in, and neither of them work well if you have commands being entered into multiple terminals at the same time. What I would like to do is capture much richer information - as an example, the command, the time it executed, the time it completed, the exit status, the first few lines of stdin and stdout. I'd also prefer to send this to a listening daemon somewhere which could happily multiplex multiple terminals. The easy way to do this is to pass the command to another program which can exec a shell to handle the command as a subprocess whilst getting handles to stdin, stdout, exit status etc. One could write a shell to do this, but you'd lose much of the functionality already in bash, which would be annoying.

这样做的动机来自于试图让喜欢这样的事实后,程序探索性数据分析的意义。有了这样丰富的信息,将有可能产生对发生了什么像样的报告,挤压一个命令的多次调用到一个地方前几个给非零退出,问在哪里文件从通过搜索感动文件都来了,等等等等。

The motivation for this comes from trying to make sense of exploratory data analysis like procedures after the fact. With richer information like this, it would be possible to generate decent reporting on what happened, squashing multiple invocations of one command into one where the first few gave non-zero exits, asking where files came from by searching for everything that touched the file, etc etc.

推荐答案

运行这个bash脚本:

Run this bash script:

#!/bin/bash
while read -e line
do
    wrapper "$line"
done

在最简单的形式,包装可以由的eval$ LINE的。你提到希望有时序,所以也许反而有时的eval$行。你想捕捉退出状态,所以这之后应行另存= $?。而且,你想捕捉标准输出的前几行,因此一些重定向是为了。等等。

In its simplest form, wrapper could consist of eval "$LINE". You mentioned wanting to have timings, so maybe instead have time eval "$line". You wanted to capture exit status, so this should be followed by the line save=$?. And, you wanted to capture the first few lines of stdout, so some redirecting is in order. And so on.

更多:乔这样认为,处理多行的bash命令被包括在内。在最简单的形式,如果与语法错误:意外的文件结束的eval回报,那么你要在继续之前提示输入另一行。更重要的是,要检查正确的bash命令,执行庆典-n<<<$行你做的之前的eval 。如果庆典-n 报道尾行中的出错,然后提示更多的输入要添加到`$线。等等。

MORE: Jo So suggests that handling for multiple-line bash commands be included. In its simplest form, if eval returns with "syntax error: unexpected end of file", then you want to prompt for another line of input before proceeding. Better yet, to check for proper bash commands, run bash -n <<<"$line" before you do the eval. If bash -n reports the end-of-line error, then prompt for more input to add to `$line'. And so on.

这篇关于巴什(或其他shell):用保鲜功能/脚本的所有命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆