发现在ubuntu和centos中有些命令还不一样,比如<<<可在centos中使用,但是ubuntu中不行
方法1,可在ubuntu和centos中使用
#!/bin/bash filenum=30 dir1=./upload_root/gdby dir2=./upload_root/bzbygz dir_merge=./download #删除旧文件 rm -f $dir_merge/*.csv dir1_select=`ls $dir1 | sort -r | head -$filenum` dir2_select=`ls $dir2 | sort -r | head -$filenum` #2个文件夹共有的 echo "两个学校共有的文件名" for i in ${dir1_select} do for file2 in ${dir2_select} do if [ "$i" = "$file2" ];then echo $i num1=$(head -1 $dir1/$i | awk -F "," ‘{print $2}‘) num2=$(head -1 $dir2/$i | awk -F "," ‘{print $2}‘) money1=$(head -1 $dir1/$i | awk -F "," ‘{print $4}‘) money2=$(head -1 $dir2/$i | awk -F "," ‘{print $4}‘) num_all=$(($num1+$num2)) money_all=$(echo $money1 $money2 | awk ‘{print $1+$2}‘) cp $dir1/$i $dir_merge sed -ie "1 s#`echo $num1`#`echo $num_all`#; 1 s#`echo $money1`#`echo $money_all`#" $dir_merge/$i #sed ‘1d‘ $dir2/$i >> $dir_merge/$i tail -n +2 $dir2/$i >> $dir_merge/$i fi done done rm -f $dir_merge/*.csve #只在$dir1中有的: echo "只在gdby中存在的文件" for i in ${dir1_select} do for file2 in ${dir2_select} do if [ "$i" != "$file2" ];then cp $dir1/$i $dir_merge fi done done #grep -q $i <<< ${dir2_select};if [[ $? -ne 0 ]];then cp $dir1/$i $dir_merge;fi;done #只在$dir2中有的: echo "只在bzbygz存在的文件" for i in ${dir2_select} do for file1 in ${dir1_select} do if [ "$i" != "$file1" ];then cp $dir2/$i $dir_merge fi done done
方法2,只能在centos中使用
#!/bin/bash filenum=30 dir1=./test1 dir2=./test2 dir_merge=./test3 #删除旧文件 rm -f $dir_merge/*.csv dir1_select=`ls $dir1 | sort -r | head -$filenum` dir2_select=`ls $dir2 | sort -r | head -$filenum` #2个文件夹共有的 for i in ${dir1_select} do grep -q $i <<< ${dir2_select} #<<<只能在centos中使用, grep -q安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0。 if [[ $? -eq 0 ]];then echo $i num1=$(head -1 $dir1/$i | awk -F "," ‘{print $2}‘) num2=$(head -1 $dir2/$i | awk -F "," ‘{print $2}‘) money1=$(head -1 $dir1/$i | awk -F "," ‘{print $4}‘) money2=$(head -1 $dir2/$i | awk -F "," ‘{print $4}‘) num_all=$(($num1+$num2)) money_all=$(echo $money1 $money2 | awk ‘{print $1+$2}‘) cp $dir1/$i $dir_merge sed -ie "1 s#`echo $num1`#`echo $num_all`#; 1 s#`echo $money1`#`echo $money_all`#" $dir_merge/$i #sed ‘1d‘ $dir2/$i >> $dir_merge/$i tail -n +2 $dir2/$i >> $dir_merge/$i fi done rm -f $dir_merge/*.csve #只在$dir1中有的: for i in ${dir1_select};do grep -q $i <<< ${dir2_select};if [[ $? -ne 0 ]];then cp $dir1/$i $dir_merge;fi;done #只在$dir2中有的: for i in ${dir2_select};do grep -q $i <<< ${dir1_select};if [[ $? -ne 0 ]];then cp $dir2/$i $dir_merge;fi;done