需求如下:

需要将3个月内的文件留在/app/tmp下,便于查询。

 

3个月以上的两种文件,按照创建的日期,移动到另外一个目录,比如/app/tmp/2011/03表示20113月份的所有文件。

 

最后这个脚本放在crontab里面,每天都执行。就是每天都会移一些文件到/app/tmp/nnnn/nn里面去,保证/app/tmp下永远是最近的3个月内的文件。

 

(这里脚本里改成了/app/test,防止测试前直接用于环境)

可能会有bug,写的逻辑也不是最精妙的,不过也算学到了一点ksh的特点。对于ksh中的算数运算和自定义函数都有了最简单的例子。

 

 
  
  1. #!/bin/ksh 
  2.  
  3. function month_to_num { 
  4.         typeset month=$1 
  5.         case $month in 
  6.                 "Jan") print 1;; 
  7.                 "Feb") print 2;; 
  8.                 "Mar") print 3;; 
  9.                 "Apr") print 4;; 
  10.                 "May") print 5;; 
  11.                 "Jun") print 6;; 
  12.                 "Jul") print 7;; 
  13.                 "Aug") print 8;; 
  14.                 "Sep") print 9;; 
  15.                 "Oct") print 10;; 
  16.                 "Nov") print 11;; 
  17.                 "Dec") print 12;; 
  18.         esac 
  19.  
  20.  
  21. file_dir="/app/test" 
  22. flag=0 
  23. oldest_file_name=`ls -lt $file_dir| grep -E "txt|xml" | tail -1 | awk '{print $9}'
  24. oldest_file_month_tmp=$(istat $file_dir/$oldest_file_name | grep modified | awk '{print $4}'
  25. oldest_file_month="`month_to_num "$oldest_file_month_tmp"`" 
  26. oldest_file_day=`istat $file_dir/$oldest_file_name | grep modified | awk '{print $5}'
  27. oldest_file_year=`istat $file_dir/$oldest_file_name | grep modified | awk '{print $NF}'
  28. now_month=`date +\%m` 
  29. now_day=`date +\%d` 
  30. now_year=`date +\%Y` 
  31. (( duration= (( now_year - oldest_file_year )) * 12 + now_month - oldest_file_month )) 
  32. echo $duration 
  33. while [[ $flag -ge 0 ]] 
  34. do 
  35.         flag=-1 
  36.         while [[ $duration -gt  3 || $duration -eq 3 && $now_day -eq $oldest_file_day  ]] 
  37.         do 
  38.             flag=0 
  39.                     if [ -d $file_dir/$oldest_file_year/$oldest_file_month/ ] 
  40.                   then 
  41.                     mv $file_dir/$oldest_file_name $file_dir/$oldest_file_year/$oldest_file_month/ 
  42.                   else 
  43.                     mkdir -p $file_dir/$oldest_file_year/$oldest_file_month 
  44.                     mv $file_dir/$oldest_file_name $file_dir/$oldest_file_year/$oldest_file_month/ 
  45.                   fi 
  46.                   oldest_file_name=`ls -lt $file_dir| grep -E "txt|xml" | tail -1 | awk '{print $9}'
  47.                   oldest_file_month_tmp=$(istat $file_dir/$oldest_file_name | grep modified | awk '{print $4}'
  48.                   oldest_file_month="`month_to_num "$oldest_file_month_tmp"`" 
  49.                   oldest_file_day=`istat $file_dir/$oldest_file_name | grep modified | awk '{print $5}'
  50.                   oldest_file_year=`istat $file_dir/$oldest_file_name | grep modified | awk '{print $NF}'
  51.                   (( duration= (( now_year-oldest_file_year )) * 12 + now_month - oldest_file_month )) 
  52.         done 
  53. done 

做如下总结:

1.ksh的算数运算应该是比较有特点的。

2.$()和反逗点"`"应该是差不多的作用。

3.最简单的自定义函数的写法和调用。

小学生水平的脚本……请大家指教