Comparison function that compares two text files in Unix

前端 未结 5 2074
走了就别回头了
走了就别回头了 2021-02-07 12:54

I was wondering if anyone could tell me if there is a function available in unix, bash that compares all of the lines of the files. If they are different it should output true/f

5条回答
  •  春和景丽
    2021-02-07 13:22

    #!/bin/bash
    
    file1=old.txt
    file2=new.txt
    
    echo " TEST 1 : "
    echo
    
    if [ $( cmp -s ${file1} ${file2}) ]
    then
       echo "The files match :  ${file1} - ${file2}"
    else
       echo "The files are different :  ${file1} - ${file2}"
    fi
    
    echo
    echo " TEST 2 : "
    echo
    bool=$(cmp -s "$file1" "$file2" )
    if cmp -s "$file1" "$file2"
    then
       echo "The files match"
    else
       echo "The files are different"
    fi
    
    echo
    echo " TEST 3 : md5 / md5sum - compute and check MD5 message digest"
    echo
    
    md1=$(md5 ${file1});
    md2=$(md5 ${file2});
    
    mdd1=$(echo $md1 | awk '{print $4}' ) 
    mdd2=$(echo $md2 | awk '{print $4}' ) 
    
    # or md5sum depends on your linux flavour :D
    #md1=$(md5sum ${file1});
    #md2=$(md5sum ${file2});
    
    #mdd1=$(echo $md1 | awk '{print $1}' ) 
    #mdd2=$(echo $md2 | awk '{print $1}' ) 
    
    echo $md1
    echo $mdd1
    echo $md2
    echo $mdd2
    echo
    
    #if [ $mdd1 = $mdd2 ]; 
    if [ $mdd1 -eq $mdd2 ]; 
    then
       echo "The files match :  ${file1} - ${file2}"
    else
       echo "The files are different :  ${file1} - ${file2}"
    fi
    

提交回复
热议问题