某日在网上溜达,看见一网友博客(http://rainbird.blog.51cto.com/211214/165779)上的两道题,作者说没有两年linux道行,很难做出其中三道。这里我们看看这些题究竟有多难(仅针对脚本编程)。

第一道:

 
  
  1. 取出/etc/passwd中shell出现的次数 

顺便贴一下我的/etc/passwd文件

 
  
  1. root:x:0:0:root:/root:/bin/bash 
  2. bin:x:1:1:bin:/bin:/sbin/nologin 
  3. daemon:x:2:2:daemon:/sbin:/sbin/nologin 
  4. adm:x:3:4:adm:/var/adm:/sbin/nologin 
  5. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
  6. sync:x:5:0:sync:/sbin:/bin/sync 
  7. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
  8. halt:x:7:0:halt:/sbin:/sbin/halt 
  9. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
  10. news:x:9:13:news:/etc/news: 
  11. uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 
  12. operator:x:11:0:operator:/root:/sbin/nologin 
  13. games:x:12:100:games:/usr/games:/sbin/nologin 
  14. gopher:x:13:30:gopher:/var/gopher:/sbin/nologin 
  15. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 
  16. nobody:x:99:99:Nobody:/:/sbin/nologin 
  17. nscd:x:28:28:NSCD Daemon:/:/sbin/nologin 
  18. vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 
  19. rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin 
  20. mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin 
  21. smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin 
  22. oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin 
  23. pcap:x:77:77::/var/arpwatch:/sbin/nologin 
  24. dbus:x:81:81:System message bus:/:/sbin/nologin 
  25. avahi:x:70:70:Avahi daemon:/:/sbin/nologin 
  26. sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 
  27. rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 
  28. nfsnobody:x:4294967294:4294967294:Anonymous NFS User:/var/lib/nfs:/sbin/nologin 
  29. haldaemon:x:68:68:HAL daemon:/:/sbin/nologin 
  30. avahi-autoipd:x:100:156:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin 
  31. xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin 
  32. ntp:x:38:38::/etc/ntp:/sbin/nologin 

这道题真的很简单,要知道处理文本那可是Perl的强项。熟悉perl的童鞋,一个hash就能搞定。

 
  
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict; 
  4.  
  5. my %hash; 
  6.  
  7. open my $file,'<','/etc/passwd' or die "$!\n"; 
  8.  
  9. while ( <$file> ) { 
  10.        chomp; 
  11.        my @shell = split /:/; 
  12.  
  13.        if ( $shell[6] ) { 
  14.           if ( exists $hash{$shell[6]} ) { 
  15.               $hash{$shell[6]}++; 
  16.           } else { 
  17.               $hash{$shell[6]} = 1; 
  18.           } 
  19.       } 
  20.  
  21.  
  22. close $file; 
  23.  
  24. while ( my ( $key,$val ) = each %hash ) { 
  25.        print "$key\t$val\n"; 

output:

 
  
  1. [root@Xen ~]# perl c.pl 
  2. /sbin/halt      1 
  3. /bin/sync       1 
  4. /sbin/nologin   27 
  5. /sbin/shutdown  1 
  6. /bin/bash       1 

第二道:

employee文件中记录了工号和姓名

 
  
  1. 100 Jason Smith 
  2. 200 John Doe 
  3. 300 Sanjay Gupta 
  4. 400 Ashok Sharma  

bonus文件中记录工号和工资

 
  
  1. 100 $5,000 
  2. 200 $500 
  3. 300 $3,000 
  4. 400 $1,250  

要求把两个文件合并并输出如下
处理结果:

 
  
  1. 400 ashok sharma $1,250 
  2. 100 jason smith  $5,000 
  3. 200 john doe  $500 
  4. 300 sanjay gupta  $3,000 

这道题也不是特别的难,两个hash就可以搞定的

 
  
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict; 
  4.  
  5. my (%A,%B); 
  6.  
  7. open my $file,'<','employee.txt' or die "$!\n"; 
  8.  
  9. while ( <$file> ) { 
  10.        chomp; 
  11.        /^(\d+)\s+\w+\s+\w+/; 
  12.        $A{$1} = $_ if $1 ; 
  13.  
  14. close $file; 
  15.  
  16. open $file,'<','bonus.txt' or die "$!\n"; 
  17.  
  18. while ( <$file> ) { 
  19.        chomp; 
  20.        /^(\d+)\s+(\$\S+)/; 
  21.        $B{$1} = $2 if ( $1 || $2 ); 
  22.  
  23. close $file; 
  24.  
  25. for my $num ( keys %A ) { 
  26.  
  27.     if ( exists $B{$num} ) { 
  28.         $A{$num} ."\t$B{$num}"
  29.     } 
  30.  
  31.  
  32. print join "\n",sort values %A; 
  33. print qq/\n/; 

output:

 
  
  1. [root@Xen ~]# perl hebin.pl 
  2. 100 Jason Smith $5,000 
  3. 200 John Doe    $500 
  4. 300 Sanjay Gupta        $3,000 
  5. 400 Ashok Sharma        $1,250 

怎么样,看完上面的代码,您有学习perl的冲动了没?