linux下对比两个文件夹下python文件的差异

1. 代码如下

#!/bin/bash
##########################################################
# Filename      : pyDiff
# Description   : 查看文件夹下python文件的不同, use like: 
#                 pyDiff dir1 dir2
#                 参考:https://vi.stackexchange.com/questions/778/how-to-diff-and-merge-two-directories
# #######################################################

#######################################
#
# r 表示 颜色是红色,支持黑红绿黄蓝白
# 默认为红色
#
#######################################

function color {
    case "$2" in 
    k) echo -e "\033[30m${1}\033[0m";;
    r) echo -e "\033[31m${1}\033[0m";; 
    g) echo -e "\033[32m${1}\033[0m";;   
    y) echo -e "\033[33m${1}\033[0m";;   
    b) echo -e "\033[34m${1}\033[0m";;   
    *) echo -e "\033[37m${1}\033[0m"
    esac
}

function colorEcho {
    if [ -z "$2" ]; then
        c='r'
    else
        c=$2
    fi

    color "$1" "$c"
}

function Diff() {
    local dir1
    local dir2
    dir1=$1
    dir2=$2

    if [ -d "$dir1" ] && [ -d "$dir2" ]; then
        for files in $(diff -rq $dir1 $dir2|grep 'differ$'|sed "s/^Files //g;s/ differ$//g;s/ and /:/g"); do 
            if [[ "${files%:*}" == *.py ]] && [[ "${files#*:}" == *.py ]]; then
                echo 'File with diff: ' ${files%:*} ' <---> ' ${files#*:}; 
            fi
        done
        local y
        read -p "Show the diffs with vimdiff, y or n? " -n 1 y
        if [ "$y" != "y" ]; then 
            exit 0
        fi
        for files in $(diff -rq $dir1 $dir2|grep 'differ$'|sed "s/^Files //g;s/ differ$//g;s/ and /:/g"); do 
            if [[ "${files%:*}" == *.py ]] && [[ "${files#*:}" == *.py ]]; then
                vimdiff ${files%:*} ${files#*:}; 
            fi
        done
    elif [ -f "$dir1" ] && [ -f "$dir2" ]; then 
        vimdiff $1 $2 
    else
        echo "$1 $2"
        colorEcho "Error!!! \$1 and \$2 must with same type( dir or file)" r
        exit 1
    fi
}

Diff $1 $2

2.用法如下:

命令行执行./pyDiff dir1 dir2

上一篇:企业实战分布式文件系统-mfs部署、文件写入与恢复、存储等


下一篇:一个骚命令防止你的文件被误删除!