2016-11-07 73 views

回答

2

如果您想檢查兩個文件是否相同,您可以檢查diff -q(或cmp)的退出代碼。這是更快,因爲它不需要找到確切的差異:

if diff -q file1 file2 > /dev/null 
then 
    echo "The files are equal" 
else 
    echo "The files are different or inaccessible" 
fi 

所有的Unix工具有一個退出代碼,通常速度更快的,更簡單,更強大的檢查,而不是捕捉和比較在它們的輸出基於文本的方式。

0

可以使用邏輯管:

對於一個命令:

diff -q file1 file2 > /dev/null && echo "The files are equal" 

或多個命令:

diff -q file1 file2 > /dev/null && { 
    echo "The files are equal"; echo "Other command" 
    echo "More other command" 
}