来看一道Perl面试题

据说这道题是QQ的,看下怎么解答为好

题目:

把当前目录下(包含子目录)下所有后缀为.sh的文件后缀变更为.shell(用PERL完成)

一、先人为的制造一些.sh文件

这个用shell吧,方便

 
 
  1. for i in `seq 100`;do touch $i.sh;done 
  2. for i in testa testb testc testd teste testf testg;do touch /home/test/$i.sh;done 

好,生成文件,OK,部分图

 
 
  1. [root@test ~]# ll /home/ 
  2. total 404 
  3. drwxr-xr-x 2 root root 4096 Mar  2 09:20 100.sh 
  4. drwxr-xr-x 2 root root 4096 Mar  2 09:20 10.sh 
  5. drwxr-xr-x 2 root root 4096 Mar  2 09:20 11.sh 
  6. drwxr-xr-x 2 root root 4096 Mar  2 09:20 12.sh 
  7. drwxr-xr-x 2 root root 4096 Mar  2 09:20 13.sh 
  8. drwxr-xr-x 2 root root 4096 Mar  2 09:20 14.sh 
  9. drwxr-xr-x 2 root root 4096 Mar  2 09:20 15.sh 
  10. drwxr-xr-x 2 root root 4096 Mar  2 09:20 16.sh 
  11. drwxr-xr-x 2 root root 4096 Mar  2 09:20 17.sh 
  12. drwxr-xr-x 2 root root 4096 Mar  2 09:20 18.sh 
  13. drwxr-xr-x 2 root root 4096 Mar  2 09:20 19.sh 
  14. drwxr-xr-x 2 root root 4096 Mar  2 09:20 1.sh 
  15. drwxr-xr-x 2 root root 4096 Mar  2 09:20 20.sh 
  16. drwxr-xr-x 2 root root 4096 Mar  2 09:20 21.sh 
  17. drwxr-xr-x 2 root root 4096 Mar  2 09:20 22.sh 
  18. drwxr-xr-x 2 root root 4096 Mar  2 09:20 23.sh 
  19. drwxr-xr-x 2 root root 4096 Mar  2 09:20 24.sh 
  20. drwxr-xr-x 2 root root 4096 Mar  2 09:20 25.sh 
  21. drwxr-xr-x 2 root root 4096 Mar  2 09:20 26.sh 
  22. drwxr-xr-x 2 root root 4096 Mar  2 09:20 27.sh 
  23. drwxr-xr-x 2 root root 4096 Mar  2 09:20 28.sh 
  24. drwxr-xr-x 2 root root 4096 Mar  2 09:20 29.sh 
  25. drwxr-xr-x 2 root root 4096 Mar  2 09:20 2.sh 
  26. drwxr-xr-x 2 root root 4096 Mar  2 09:20 30.sh 
  27. drwxr-xr-x 2 root root 4096 Mar  2 09:20 31.sh 
  28. drwxr-xr-x 2 root root 4096 Mar  2 09:20 32.sh 
  29. drwxr-xr-x 2 root root 4096 Mar  2 09:20 33.sh 
  30. drwxr-xr-x 2 root root 4096 Mar  2 09:20 34.sh 
  31. drwxr-xr-x 2 root root 4096 Mar  2 09:20 35.sh 
  32. drwxr-xr-x 2 root root 4096 Mar  2 09:20 36.sh 
  33. drwxr-xr-x 2 root root 4096 Mar  2 09:20 37.sh 
  34. drwxr-xr-x 2 root root 4096 Mar  2 09:20 38.sh 
  35. drwxr-xr-x 2 root root 4096 Mar  2 09:20 39.sh 
  36. drwxr-xr-x 2 root root 4096 Mar  2 09:20 3.sh 
  37. drwxr-xr-x 2 root root 4096 Mar  2 09:20 40.sh 
  38. drwxr-xr-x 2 root root 4096 Mar  2 09:20 41.sh 
  39. drwxr-xr-x 2 root root 4096 Mar  2 09:20 42.sh 
  40. drwxr-xr-x 2 root root 4096 Mar  2 09:20 43.sh 
  41. drwxr-xr-x 2 root root 4096 Mar  2 09:20 44.sh 
  42. drwxr-xr-x 2 root root 4096 Mar  2 09:20 45.sh 
  43. drwxr-xr-x 2 root root 4096 Mar  2 09:20 46.sh 

/home/test下

 
 
  1. [root@test ~]# ll /home/test/ 
  2. total 0 
  3. -rw-r--r-- 1 root root 0 Mar  2 09:48 testa.sh
  4. -rw-r--r-- 1 root root 0 Mar  2 09:48 testb.sh
  5. -rw-r--r-- 1 root root 0 Mar  2 09:48 testc.sh
  6. -rw-r--r-- 1 root root 0 Mar  2 09:48 testd.sh
  7. -rw-r--r-- 1 root root 0 Mar  2 09:48 teste.sh
  8. -rw-r--r-- 1 root root 0 Mar  2 09:48 testf.sh
  9. -rw-r--r-- 1 root root 0 Mar  2 09:48 testg.sh

写perl 脚本,记得perl有一个模块叫File::Find,可以完成这个效果,试下

code:

 
 
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict; 
  4. use File::Find; 
  5.  
  6. sub wanted { 
  7.  
  8.     if (-f $File::Find::name) { 
  9.  
  10.         if ($File::Find::name =~ m/\.sh$/) { 
  11.             #print $File::Find::name,"\n"; 
  12.             #print $_,"\n"; 
  13.             #print $File::Find::dir,"\n"; 
  14.             $File::Find::name =~ s/\.sh$/\.shell/g; 
  15.             rename $_,$File::Find::name; 
  16.         } 
  17.  
  18.     } 
  19.  
  20.  
  21. find(\&wanted,'/home'); 

运行一下,测试,大喜,完成,部分图

 
 
  1. [root@WEB-01 ~]# ll /home/ 
  2. total 4 
  3. -rw-r--r-- 1 root root    0 Mar  2 09:47 100.shell 
  4. -rw-r--r-- 1 root root    0 Mar  2 09:47 10.shell 
  5. -rw-r--r-- 1 root root    0 Mar  2 09:47 11.shell 
  6. -rw-r--r-- 1 root root    0 Mar  2 09:47 12.shell 
  7. -rw-r--r-- 1 root root    0 Mar  2 09:47 13.shell 
  8. -rw-r--r-- 1 root root    0 Mar  2 09:47 14.shell 
  9. -rw-r--r-- 1 root root    0 Mar  2 09:47 15.shell 
  10. -rw-r--r-- 1 root root    0 Mar  2 09:47 16.shell 
  11. -rw-r--r-- 1 root root    0 Mar  2 09:47 17.shell 
  12. -rw-r--r-- 1 root root    0 Mar  2 09:47 18.shell 
  13. -rw-r--r-- 1 root root    0 Mar  2 09:47 19.shell 
  14. -rw-r--r-- 1 root root    0 Mar  2 09:47 1.shell 
  15. -rw-r--r-- 1 root root    0 Mar  2 09:47 20.shell 
  16. -rw-r--r-- 1 root root    0 Mar  2 09:47 21.shell 
  17. -rw-r--r-- 1 root root    0 Mar  2 09:47 22.shell 
  18. -rw-r--r-- 1 root root    0 Mar  2 09:47 23.shell 
  19. -rw-r--r-- 1 root root    0 Mar  2 09:47 24.shell 
  20. -rw-r--r-- 1 root root    0 Mar  2 09:47 25.shell 
  21. -rw-r--r-- 1 root root    0 Mar  2 09:47 26.shell 
  22. -rw-r--r-- 1 root root    0 Mar  2 09:47 27.shell 
  23. -rw-r--r-- 1 root root    0 Mar  2 09:47 28.shell 
  24. -rw-r--r-- 1 root root    0 Mar  2 09:47 29.shell 
  25. -rw-r--r-- 1 root root    0 Mar  2 09:47 2.shell 
  26. -rw-r--r-- 1 root root    0 Mar  2 09:47 30.shell 
  27. -rw-r--r-- 1 root root    0 Mar  2 09:47 31.shell 
  28. -rw-r--r-- 1 root root    0 Mar  2 09:47 32.shell 
  29. -rw-r--r-- 1 root root    0 Mar  2 09:47 33.shell 
  30. -rw-r--r-- 1 root root    0 Mar  2 09:47 34.shell 
  31. -rw-r--r-- 1 root root    0 Mar  2 09:47 35.shell 
  32. -rw-r--r-- 1 root root    0 Mar  2 09:47 36.shell 
  33. -rw-r--r-- 1 root root    0 Mar  2 09:47 37.shell 
  34. -rw-r--r-- 1 root root    0 Mar  2 09:47 38.shell 
  35. -rw-r--r-- 1 root root    0 Mar  2 09:47 39.shell 

/home/test下

 
 
  1. [root@WEB-01 ~]# ll /home/test/ 
  2. total 0 
  3. -rw-r--r-- 1 root root 0 Mar  2 09:48 testa.shell 
  4. -rw-r--r-- 1 root root 0 Mar  2 09:48 testb.shell 
  5. -rw-r--r-- 1 root root 0 Mar  2 09:48 testc.shell 
  6. -rw-r--r-- 1 root root 0 Mar  2 09:48 testd.shell 
  7. -rw-r--r-- 1 root root 0 Mar  2 09:48 teste.shell 
  8. -rw-r--r-- 1 root root 0 Mar  2 09:48 testf.shell 
  9. -rw-r--r-- 1 root root 0 Mar  2 09:48 testg.shell 

呵,perl 的File::Find模块有三个特殊的变量

 
 
  1. $_                 包含目录中的当前文件名 
  2. $File::Find::dir   包含当前目录名 
  3. $File::Find::name  包含$File::Find::dir/$_ 

其他的功能尚待研究ing.谢谢大家。

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...