zoukankan      html  css  js  c++  java
  • shell函数递归调用实现目录的对比拷贝

    目的与需求:

    在移植时,需要将根文件系统rootfs1中的新增内容合入rootfs2,但不能覆盖rootfs2中原有的东西。即只能比较两个文件系统的异同,将1中比2中多出来的东西移到2中。

    难点:

    目录中若有相同的子目录,也要使得子目录满足上述需求。

    点睛之笔:

    shell 递归调用, 借鉴二叉树的深度优先遍历。

    实操:

     1 #!/bin/bash
     2 
     3 cpdifffile()
     4 {
     5 
     6     DIR1=$1
     7     DIR2=$2
     8     files1=`ls $DIR1`
     9     #files2=`ls $DIR2`
    10     for file1 in $files1
    11     do
    12         ls $DIR2/$file1 > /dev/null 2>&1
    13         if [ $? -eq 0 ]
    14         then
    15             echo $DIR2 have file $file1
    16         else
    17             echo will cp $DIR1/$file1 to $DIR2
    18             cp -a $DIR1/$file1 $DIR2/
    19         fi
    20     done
    21 }
    22 
    23 cpsamedir()
    24 {
    25     cpdifffile $1 $2
    26     subdirs1=`ls -l $1 | grep "^d" | awk -F  '{print $9}'`
    27     subdirs2=`ls -l $2 | grep "^d" | awk -F  '{print $9}'`
    28     unset samedirs ##注意,此处要清空数组,否则上次调用的结果保存在这里将会形成环状,死循环
    29     i=0
    30     for subdir1 in $subdirs1
    31     do
    32         ls $2/$subdir1 > /dev/null 2>&1
    33         if [ $? -eq 0 ]
    34         then
    35             samedirs[i]=$subdir1
    36             let i++
    37 
    38         else
    39             echo  $1 have dir $subdir1, but  $2 not.
    40         fi
    41     done
    42     echo "bettenw $1 and $2, the same dir is:"
    43     echo ${samedirs[@]}
    44 
    45     for samedir in ${samedirs[@]}
    46     do
    47         echo "now oparate $1/$samedir and $2/$samedir"
    48         cpsamedir $1/$samedir $2/$samedir
    49     done
    50 
    51 
    52 
    53 }
    54 
    55 main()
    56 {
    57     cpsamedir $1 $2
    58 
    59 }
    60 
    61 main $1 $2
  • 相关阅读:
    [转载]JavaScript世界的一等公民 函数
    JavaScript基础知识词法结构
    JavaScript触发asp.net服务器端控件事件
    WinForm窗体间传值大全
    快速导入Excel
    让虚拟机支持USB HDD 启动
    常见的mysql导出excel工具介绍
    silverlight 播放器,丑丑版
    快速导出Excel
    一个C#编写QQ接口软件QQ协议(转)
  • 原文地址:https://www.cnblogs.com/xxg1992/p/8858151.html
Copyright © 2011-2022 走看看