#/bin/ksh            
##Create Log Directory 
if [ -d $HOME/rmdtab ] 
then
 tempdirname=$HOME/rmdtab/$( date +"%Y%h%d.%H%M%S")
 mkdir $tempdirname
else
 echo " Directory is not exists !"
 exit
fi                   
tempdirname=$HOME/rmdtab/$( date +"%Y%h%d%H%M%S")
mkdir $tempdirname;
##Get Job File List ,and Sort File List.  
setdtab show > $tempdirname/originfile.txt; 
#Collect File List ,and Exclude RD Job File List.
grep -v "^[0-3] " $tempdirname/originfile.txt | grep  "/eb[1-99]/ebtest/pattern/." |grep -v "/eb[1-99]/ebtest/pattern/RD/."> $tempdirname/2stfile.txt 
grep -v "^[0-3] " $tempdirname/originfile.txt | grep  -v "/eb[1-99]/ebtest/pattern/." > $tempdirname/notreatfile.txt
#Get Directory Name
cut -d " " -f 2 $tempdirname/2stfile.txt > $tempdirname/sortrawdata.txt
## Read File List File for sortrawdata,and Separator Job File name by space.
temp=" ";
while read myfile; do 
 temp=$temp$myfile" "; 
done < $tempdirname/sortrawdata.txt   
##Sort Job File List by Date , Check Job File exists ,and input sortdata.txt.
ls -td $temp > $tempdirname/sortdata.txt;     
## Read File List from sortdata.txt ,Delete Job File Directory ,and Remove File List from System. 
typeset -i tempindex=1;
tempnum=" ";
var=" ";
#Read Job File from sortdata.txt
exec 3< $tempdirname/sortdata.txt
while read -r mylines <&3;  
 do                       
 #only delete 20 Job File by sort date.
 if [ tempindex -gt 20 ]
 then
 
  #Check Job File in Sytem List ,and wait User Response.  
   tempnum=$tempnum$(grep $mylines $tempdirname/2stfile.txt | cut -d " " -f 1 )" ";
   print -n "Delete "$mylines" (Y/N) \n"; read var;
 
   if [ $var = "y" ] 
   then
  
     echo $tempnum;
  
     #Delete Job File From System and Disk.
     setdtab remove  $tempnum;
           echo $mylines Delete!"
     rm -r $mylines;
  
    else
  
     echo $mylines" Not Delete!";
 
   fi
 fi
 
  var=" ";
  tempindex=tempindex+1;   
 
done  
grep -v "^[0-3] " $tempdirname/originfile.txt | grep   "/eb[1-99]/ebtest/pattern/RD" > $tempdirname/rdfile.txt
#Delete RD Job FIle.
exec 3< $tempdirname/rdfile.txt
while read -r myliness <&3;  
 do 
 if [ tempindex -gt 20 ]
 then
   tempnum=$tempnum$(grep $myliness $tempdirname/rdfile.txt | cut -d " " -f 1 )" ";
  print -n "Delete "$myliness" (Y/N) \n"; read var;
 
   if [ $var = "y" ] 
   then   
 
     echo $tempnum;
     setdtab remove  $tempnum;
    echo $myliness" Delete!" 
  
    else              
  
     echo $myliness" Not Delete!"  
  
   fi
  fi     
 
  var=" ";
  tempindex=tempindex+1; 
 
done  
echo $tempnum;