3

I am using the following command without success -

$ google-chrome <(grep "simple" SimpleDoc.txt)

If I simply do a grep, I get -

$ grep "simple" SimpleDoc.txt
Very simple doc that contains plaintext. 

I also wrote a python script called showFileContents.py to test process substitution and it works -

#! /usr/bin/python

import sys

arg1 = sys.argv[1]
f = open(arg1)
line = f.readline()

while line:
    sys.stdout.write(line)
    line = f.readline()

f.close()

Here's the output -

$ showFileContents.py <(grep "simple" SimpleDoc.txt)
Very simple doc that contains plaintext.

But when I try to do the same with

$ google-chrome <(grep "simple" SimpleDoc.txt)

the browser window shows nothing. With Firefox, I get an error saying that "Firefox can't find the file".

Why is this happening and how can I use process substitution with chrome?

2 Answers 2

6

When you run google-chrome, the process detects that there is an open instance of Chrome and sends it a message to open the file. The message contains the file name. Firefox works in the same way. The process that's started from the shell is not the already-running browser process.

The <(…) construct works by creating an anonymous pipe. It is passed to the command with a name like /dev/fd/42, meaning “the file that's already open on descriptor 42”. This file can only be accessed by a process that was started to execute your command line, not by the already-running browser process.

In order to pass the data to the running browser process, the data must be in a file, with a name that both processes can access. With Firefox, you can use a named pipe:

mkfifo f
grep "simple" SimpleDoc.txt >f &
firefox f
rm f

Chrome doesn't seem to be able to read from a pipe, so you have to create a temporary file. You might as well do this for Firefox too.

In zsh, the variant process susbtitution construct =() creates a temporary file.

google-chrome =(grep "simple" SimpleDoc.txt)

In bash, you have to create a temporary file manually.

tmp=$(mktemp)
grep "simple" SimpleDoc.txt >$tmp
google-chrome "$tmp"
rm "$tmp"
1

Well you always have the option of:

grep "simple" SimpleDoc.txt > delme.txt; google-chrome delme.txt
1
  • Of course, but the whole point is to avoid creating a temporary file on disk.
    – CodeBlue
    Commented Jul 24, 2013 at 20:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .