shell-数组排序

1,数组排序

#!/bin/bash

folders=(1 3 2 9 7 10 6)
len=${#folders[@]}

for((i=0; i<$len; i++)){
  for((j=i+1; j<$len; j++)){

    if [[ ${folders[i]} -gt ${folders[j]} ]]
    then
      temp=${folders[i]}
      folders[i]=${folders[j]}
      folders[j]=$temp
    fi

  }
}

echo ${folders[@]}

  

 

项目备份:

cp repository/fxserver.war backup/fxserver_$t.war
sh /home/admin/backup/cleanup.sh

 

#功能:维护项目备份文件的数量,保留最新文件(根据文件名中的日期)

#保留文件个数
num=15
#文件路径
cd /home/admin/backup
#文件命名格式,以14位日期和.war
#*20161101024543.war

j=0
for i in `ls`
do
  var=$i
  varlen=`expr length $i`
  if [ $varlen -lt 18 ]
  then continue
  fi
  folders[j]=${var:0-18:14}
  j=`expr $j + 1`
done

len=${#folders[@]}
if [ "$len" -le $num ]; then
 exit 
else
  morenum=`expr $len - $num`
  for((i=0; i<$len; i++)){
    for((j=i+1; j<$len; j++)){
         
      if [[ ${folders[i]} -gt ${folders[j]} ]]
      then
        temp=${folders[i]}
        folders[i]=${folders[j]}
        folders[j]=$temp
      fi
    
    }
  }
  for((e=0; e<$morenum; e++)){  
    rm -f *${folders[e]}.war 
  } 
fi 

 

你可能感兴趣的:(java)