如何编辑命令输出并执行它? [英] How to edit a command output and execute it?

查看:125
本文介绍了如何编辑命令输出并执行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在标准编辑器中编辑命令

但我没有找到如何编辑命令的OUTPUT并在命令行中执行。
例如,如果我键入:

But I don't found how to edit OUTPUT of command and execute it in command line. By example, if I type:


remake

remake

我获得:


El programa«remake»noestáinstalado。
sudo apt-get install remake

El programa «remake» no está instalado. Puede instalarlo escribiendo: sudo apt-get install remake

我喜欢删除第一行,执行第二行。

I like remove de first line, and execute the second one.

这是特殊情况,因为输出是fron stderr而不是粗壮。

This is special case because output is fron stderr instead of stout.

但在其他情况下是有用的编辑输出,添加命令并执行它。

But in other cases is usefull edit output, add command and execute it.

推荐答案

看到你的评论,答案将取决于你想要做什么。

Seeing your comment, the answer will depend on what you want to do with your output.

首先,您必须保存输出;

First of all, you have to save the output;

可以将其保存在vi中打开的临时文件中:

Extensively, you can save it in a temp file you open in vi :

TMPFILE=`mktemp`
my-command > $TMPFILE
vi $TMPFILE
// do whatever you want with $TMPFILE :
// source $TMPFILE // to execute it

可存储为函数

function editrun() {
    TMPFILE=`mktemp`
    "$@" > $TMPFILE 2>&1 # run the command, redirects stdout to $TMPFILE, and also stderr
    vi $TMPFILE
    source $TMPFILE
}

然后你可以调用

editrun my shell command with arguments

例如:

editrun echo ls -al

获取并运行命令行,我用$ @。这应该在几乎任何情况下工作;但是,应该可以使用一些特殊的bash变量获取完整的bash命令行。

To get and run the command line, I used "$@". This should work in almost any situation; however, it should be possible to get the full bash command-line with some special bash variables.

这篇关于如何编辑命令输出并执行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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