首页 > 编程语言 > 详细

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

时间:2021-05-08 23:19:20      阅读:23      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/lyg-blog/p/14746170.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!