进程替换 [英] Process substitution

查看:200
本文介绍了进程替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给看看周围什么困扰我,我才发现这一点:
<一href=\"http://stackoverflow.com/questions/4274171/do-some-programs-not-accept-process-substitution-for-input-files\">Do某些程序不接受输入的文件,进程替换?

I've given a look around about what puzzles me and I only found this: Do some programs not accept process substitution for input files?

这部分帮助,但我真的想了解完整的故事。
我注意到,我的一些ř脚本给出不同的(即错误的)结果当我使用过程中替换。

which is partially helping, but I really would like to understand the full story. I noticed that some of my R scripts give different (ie. wrong) results when I use process substitution.

我试图找出与一个测试用例的问题:

I tried to pinpoint the problem with a test case:

本脚本:

#!/usr/bin/Rscript

args  <- commandArgs(TRUE)
file  <-args[1]
cat(file)
cat("\n")
data <- read.table(file, header=F)
cat(mean(data$V1))
cat("\n")

以这种方式产生的输入的文件:

with an input file generated in this way:

$ for i in `seq 1 10`; do echo $i >> p; done
$ for i in `seq 1 500`; do cat p >> test; done

让我这样的:

$ ./mean.R test
test
5.5

$ ./mean.R <(cat test)
/dev/fd/63
5.501476

进一步测试显示,某些行丢失了......但我想明白为什么。函数read.table是否(扫描给出了相同的结果)使用求?

Further tests reveal that some lines are lost...but I would like to understand why. Does read.table (scan gives the same results) uses seek?

诗。
用较小的测试文件报道(100)的错误:

Ps. with a smaller test file (100) an error is reported:

$./mean.R <(cat test3)
/dev/fd/63
Error in read.table(file, header = F) : no lines available in input
Execution halted

添加#1:与使用扫描结果是相同的一个修改后的脚本

Add #1: with a modified script that uses scan the results are the same.

推荐答案

我写了这个通用的功能,打开我自己的脚本文件连接:

I have written this general purpose function for opening a file connection in my own scripts:

OpenRead <- function(arg) {

   if (arg %in% c("-", "/dev/stdin")) {
      file("stdin", open = "r")
   } else if (grepl("^/dev/fd/", arg)) {
      fifo(arg, open = "r")
   } else {
      file(arg, open = "r")
   }
}

在您的code,替换文件文件&LT; - 打开读取(文件),它应该处理所有的如下:

In your code, replace file with file <- OpenRead(file) and it should handle all of the below:

./mean.R test
./mean.R <(cat test)
cat test | ./mean.R -
cat test | ./foo.R /dev/stdin

这篇关于进程替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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