LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2051|回复: 0

使用管道|的2个例子。

[复制链接]
发表于 2003-12-10 15:23:47 | 显示全部楼层 |阅读模式
#!/usr/bin/perl -w
#file name : pipe_example
open(WC," | wc -l ") or die "can not open process";#用open打开文件并返回一个文件句柄给WC.,不成功就显示can not open process并退出程序。
print WC "one\n"; #向句柄写如下的one ,two.three.最后用wc -l 处理它们。
print WC "two\n";
print WC "three\n";
close WC; #关闭句柄。

运行是这样:
perl pipe_example
3

如果管道符号位于程序名的前头,则打开文件句柄用于写,并且写向文件句柄的任何内容都发给程序的标准输入。如果管道符号紧跟着程序名,则打开文件句柄用于读,并且把内容发送到标准输出。

一个管道在程序名后的例子:
#!/usr/bin/perl -w
open(FILE , "ls -l |" ) or die "can not open ls -l : $! " ;
while (my $line = <FILE>) {#从文件句柄读取内容并输出到标准输出。
print "$line\n";
}
close FILE ;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表