从使用xargs执行的命令的stdin读取 [英] reading in from stdin from a command executed with xargs

查看:79
本文介绍了从使用xargs执行的命令的stdin读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用xargs做了我没想到的事情,尽管我认为这很有意义.这不是我所做的,但这是一个示例,应显示发生的情况.

Using xargs did something I didn't quite expect, though I guess it sort of makes sense. This is not what I did, but this is an example which should show what happened.

#!/usr/bin/bash
index=1
for arg in "$@"; do echo "Arg #$index = '$arg'"; let ++index; done
read -p "type something followed by enter: "  a
echo "You typed '$a'."

现在是命令:

echo boo hoo | xargs ./fn.sh

现在我想要的是fn.sh可以从stdin读取以允许用户交互,但是xargs已篡改了该内容.我想我可以从临时文件中读取xargs,但是我想知道它是否可以使用未命名的文件.

Now what I want is that fn.sh can read from stdin to allow user interaction, but that's been usurped by xargs. I guess I could get xargs to read from a temporary file, but I was wondering if it can use an unnamed file.

推荐答案

我从未使用过cygwin,但是通常我会这样做:

I've never used cygwin, but normally I'd do something like this:

xargs -a <(echo boo hoo) ./fn.sh

-a告诉xargs从文件中读取,而<( )语法(可能与cygwin一起使用或可能不起作用)是进程替换,它有效地创建了一个命名对象(命名管道或以<开头的路径c7>)可以读取,产生运行附带命令的结果.

-a tells xargs to read from a file, and the <( ) syntax (which might or might not work with cygwin) is process substitution, which effectively creates a named object (either a named pipe or a path starting /dev/fd) which can be read, yielding the result of running the enclosed command.

这不如管道语法方便,因为您必须将数据源放在xargs命令的中间,但在其他方面是等效的.

That's not as convenient as pipe syntax, since you have to put the data source in the middle of the xargs command, but it's otherwise equivalent.

这篇关于从使用xargs执行的命令的stdin读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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