tags:

views:

56

answers:

2

Hi,

Why does the following not work?

exec 3<|cat $0

The idea is to get file-descriptor (3) of a pipe (| cat $0).

+2  A: 

You cannot mix exec and pipes that way. It seems what you need is a process substitution:

$ exec 3< <(cat /etc/hosts)
$ grep ftp <&3
209.85.41.143 ftp.archlinux.org

http://tldp.org/LDP/abs/html/x17601.html#REDIR1

http://tldp.org/LDP/abs/html/process-sub.html

tokland
Of course, this serves as a generic example, if you need only a "cat", you can write directly: "exec 3< myfile".
tokland
A: 
exec 3<>$0
while read -u 3 line;
do
  grep ftp $line;
done
exec 3>&-;
krico
this makes 3 a fd of $0 file - not a pipe. See the answer.
name