ファイルの違いを表示するスクリプト

ファイルの違いを表示するスクリプト

2つのファイルの違いを見つけようとしていますが、何も生成できません。これは私がしたことです。

#Compare files and redirect output
ACTUAL_DIFF=`diff file.current file.previous 2>/dev/null`
if [ $? -ne 0 ]
    then
        echo "Comparing /tmp/file.current state (<) to /tmp/file.previous state (>), difference is: "
        echo ""
        echo $ACTUAL_DIFF | cut -f2,3 -d" "
        echo ""
    else
        echo "The current file state is identical to the previous run (on `ls -l file.previous | cut -f6,7,8 -d" "`) OR this is the initial run!"
        echo ""
    cat /tmp/file.diff
        echo ""
fi

ベストアンサー1

変数を引用符で囲む必要があります。それ以外の場合、改行文字は単語区切り文字として扱われ、すべての出力が1行に表示されます。

echo "$ACTUAL_DIFF" | cut -f2,3 -d" "

別の方法は、変数を使用せずdiffに次に直接パイプすることですcut

diff file.current file.previous 2>/dev/null | cut -f2,3 -d" "

cmp最初は、より簡単なコマンドを使用してファイルが異なるかどうかをテストできます。

出力をファイルに保存して表示するには、このteeコマンドを使用します。

#Compare files and redirect output
ACTUAL_DIFF=`diff file.current file.previous 2>/dev/null`
if ! cmp -s file.current file.previous 2>/dev/null
then
    echo "Comparing /tmp/file.current state (<) to /tmp/file.previous state (>), difference is: "
    echo ""
    diff file.current file.previous 2>/dev/null | cut -f2,3 -d" " | tee /tmp/file.diff
    echo ""
else
    echo "The current file state is identical to the previous run (on `ls -l file.previous | cut -f6,7,8 -d" "`) OR this is the initial run!"
    echo ""
    cat /tmp/file.diff
    echo ""
fi

おすすめ記事