免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: sousi

perl问题请教 [复制链接]

论坛徽章:
0
发表于 2006-12-18 20:22 |显示全部楼层
原帖由 ulmer 于 2006-12-18 19:20 发表


Hi,

using backstick operator "`":
$studout = `YOUR_SHELL_CMD`:
or using open FH -| or |- method to get STDOUT

try again. --ulmer


我试了嵌入shell,但是得不到我想要得,可不可以给我一个详细得提示,谢谢!
直接使用命令:
#ls -l | awk '{print $6}'
Dec
Dec
Dec
Dec

perl:

#!/usr/bin/perl
$test = `ls -l /opt/checkpath/test | awk \'{print $6}\'`;
print "$test\n";

结果:
[root@rhas3 perl]# perl pwd.pl
total 12
lrwxrwxrwx    1 root     root           25 Dec 18 19:49 BLOCK -> /mnt/home/dsnsv00/disk003
drwxr-xr-x    3 root     root         4096 Dec 15 14:26 dir1
drwxr-xr-x    2 root     root         4096 Dec 15 11:10 dir2
-rw-r--r--    1 root     root           52 Dec 18 19:47 file1

我希望能用STDOUT,但是不会使用,可以给我一个例子么?

论坛徽章:
0
发表于 2006-12-18 20:43 |显示全部楼层

回复 21楼 sousi 的帖子

try this way: (only awk's variable "$x" should be escaped)

$test = `ls -l /opt/checkpath/test | awk '{print \$6}'`;
print "$test\n";

--ulmer

论坛徽章:
0
发表于 2006-12-18 22:31 |显示全部楼层
[quote]原帖由 [i]ulmer[/i] 于 2006-12-18 20:43 发表
try this way: (only awk's variable "$x" should be escaped)

$test = `ls -l /opt/checkpath/test | awk '{print \$6}'`;
print "$test\n";

--ulmer [/quote]

新的问题又产生了,都不好意思问了。
#!/usr/bin/perl
$test = `ls -l /opt/checkpath/test/BLOCK | awk '{print \$9,\$10,\$11}'`;
print "$test\n";
print "$test\n",if $test = ~/\/home\/dsn/;

运行后结果:
[root@rhas3 perl]# perl pwd.pl
/opt/checkpath/test/BLOCK -> /mnt/home/dsnsv00/disk003

4294967295

怎么会出一串数字啊,结果应该是一样的啊。这个和操作系统和perl版本有关系么,公司的是solaris的。我现在家里是redhat as3。

论坛徽章:
0
发表于 2006-12-18 22:35 |显示全部楼层
#!/usr/bin/perl
use File::Find;
find(\&filefind,"/opt/checkpath/test");
sub filefind{
    my $findfile = $File::Find::name;
    if (-T $findfile){
       open(filehand,"$findfile");
       my $linenum = 1 ;
       while ($line=<filehand>)
           { print "the config file","\"$findfile\""," line ","$linenum"," includes a invaild path:\n","$line" if $line=~/\/home\/dsn/;
           $linenum = $linenum + 1;
           }
       close(filehand);
       }
      elsif (-l $findfile){
          my $test = `ls -l $findfile | awk '{print \$9,\$10,\$11}'`;
          print "test file is $test\n";
          print "the file ","\"$findfile\"","is a link file ","and includes a invaild path:\n $test\n",if $test = ~/\/home\/dsn/;
          }
}

为什么这个程序的上部分一样的匹配不会出现这样的数字问题?

这个程序运行的结果为:
[root@rhas3 perl]# perl test.pl
the config file"/opt/checkpath/test/file1" line 1 includes a invaild path:
/home/dsnsv00disk003
the config file"/opt/checkpath/test/file1" line 3 includes a invaild path:
test /home/dsnsv10/disk010
test file is /opt/checkpath/test/BLOCK -> /mnt/home/dsnsv00/disk003

the file "/opt/checkpath/test/BLOCK"is a link file and includes a invaild path:
4294967295

[ 本帖最后由 sousi 于 2006-12-18 22:38 编辑 ]

论坛徽章:
0
发表于 2006-12-19 00:14 |显示全部楼层

回复 24楼 sousi 的帖子

why don't you use regex to match STDOUT?
$stdout = `ls -l $dir`;
# put stdout into an array ued by split "\n"
@result = split "\n", $stdout;
for (@result) {
    my ($rwx, $flag, $user, $grp, $bytes, $date, $time, $filename) =  split /\s+/;
     
   # do what you want.
}

论坛徽章:
0
发表于 2006-12-19 14:27 |显示全部楼层

回复 25楼 ulmer 的帖子

谢谢你的指导,在我家运行出现数字,在公司的机器上竟然可以,不知道是什么原因,我家里用的是VMWare跑的linux不知道和这个用关系么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP