如何在当前shell中执行一个命令的输出? [英] How to execute the output of a command within the current shell?

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

问题描述

我深知(又名)工具,这将需要的内容从文件和当前shell中执行它们。

现在,我改变一些文本shell命令,然后运行它们,如下所示:

  $ LS | SED ... | SH

LS 只是一个随便举个例子,原来的文字可以是任何东西。 SED 过,只是改变文本的例子。感兴趣的是 SH 。我管不管我到 SH ,并运行它。

我的问题是,这意味着开始一个新的子壳。我宁愿我当前的shell中运行的命令。像我就可以用做一些源文件,如果我有一个文本文件中的命令。

我不希望创建一个临时文件,因为感觉很脏。

另外,我想开始我的贝子具有完全相同的特性我当前的shell。

更新

好吧,在解决方案中使用反引号一定的工作,但我经常需要做到这一点,而我检查和改变输出,所以我更preFER如果有一种方法来管的结果到的东西结束。

伤心更新

啊,的/ dev /标准输入东西看起来那么pretty,但是,在一个更​​复杂的情况下,也没有工作。

所以,我有这样的:

 找到。型的F -iname'* .DOC| ACK -v'\\ $名为.doc| perl的-pe的/ ^((*)\\ DOC。)$ / git的MV -f $ 1 $ 2.doc /我'|源的/ dev /标准输入

这确保所有的 .DOC 文件已分机小写。

和它顺便说一句,可以用的xargs 进行处理,但这是除了点。

 找到。型的F -iname'* .DOC| ACK -v'\\ $名为.doc| perl的-pe的/ ^((*)\\ DOC。)$ / $ 1 $ 2.doc /我'| xargs的-L1混帐MV

所以,当我跑前,它会立刻退出,什么都不会发生。


解决方案

  $ LS | SED ... |源的/ dev /标准输入

更新:这个工程在bash 4.0,以及tcsh的,和破折号(如果你改变 )。显然,这是在bash 3.2越野车。从 bash的4.0版本说明


  

修正了'错误'。失败读取和非正规文件,如设备或命名管道执行命令。


I'm well aware of the source (aka .) utility, which will take the contents from a file and execute them within the current shell.

Now, I'm transforming some text into shell commands, and then running them, as follows:

$ ls | sed ... | sh

ls is just a random example, the original text can be anything. sed too, just an example for transforming text. The interesting bit is sh. I pipe whatever I got to sh and it runs it.

My problem is, that means starting a new sub shell. I'd rather have the commands run within my current shell. Like I would be able to do with source some-file, if I had the commands in a text file.

I don't want to create a temp file because feels dirty.

Alternatively, I'd like to start my sub shell with the exact same characteristics as my current shell.

update

Ok, the solutions using backtick certainly work, but I often need to do this while I'm checking and changing the output, so I'd much prefer if there was a way to pipe the result into something in the end.

sad update

Ah, the /dev/stdin thing looked so pretty, but, in a more complex case, it didn't work.

So, I have this:

find . -type f -iname '*.doc' | ack -v '\.doc$' | perl -pe 's/^((.*)\.doc)$/git mv -f $1 $2.doc/i' | source /dev/stdin

Which ensures all .doc files have their extension lowercased.

And which incidentally, can be handled with xargs, but that's besides the point.

find . -type f -iname '*.doc' | ack -v '\.doc$' | perl -pe 's/^((.*)\.doc)$/$1 $2.doc/i' | xargs -L1 git mv

So, when I run the former, it'll exit right away, nothing happens.

解决方案

$ ls | sed ... | source /dev/stdin

UPDATE: This works in bash 4.0, as well as tcsh, and dash (if you change source to .). Apparently this was buggy in bash 3.2. From the bash 4.0 release notes:

Fixed a bug that caused `.' to fail to read and execute commands from non-regular files such as devices or named pipes.

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

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