Linux

需要一個命令來顯示回顯“成功”

  • August 22, 2016
diff -q file_1 File_2 || echo "success" && diff -q file_3 File_4 || echo "success" 

使用了這個命令,但這不是我需要的正確邏輯,必須編輯一些方法。

Here file_1 File_2are different and file_3 File_4 is different ,並且 echo 將執行兩次,應該只執行一次,如果文件 file_1 File_2 或 file_3 File_4 中的一個不同,則也echo "success"應該只執行一次。

如果您堅持短路評估,請使用命令分組{}

{ diff -q f1 f2 && diff -q f3 f4 ;} && echo 'Matched' || echo 'Different'

子shell()也可以:

( diff -q f1 f2 && diff -q f3 f4 ) && echo 'Matched' || echo 'Different'

如果您不想要diff’s 的輸出:

{ diff -q f1 f2 && diff -q f3 f4 ;} >/dev/null && echo 'Matched' || echo 'Different'

( diff -q f1 f2 && diff -q f3 f4 ) >/dev/null && echo 'Matched' || echo 'Different'

您最好將其放入if構造中以使其更清晰,這將使您可以輕鬆辨識文件:

if diff -q f1 f2 >/dev/null; then
   if diff -q f3 f4 >/dev/null; then
       echo 'All Matched'
   else
       echo 'f3 and f4 are Different'
   fi
else
   echo 'f1 and f2 Different'
fi

引用自:https://unix.stackexchange.com/questions/304947