簡體   English   中英

Bash,使用cmp時如何打印出來的文件是一樣的

[英]Bash, how to print out that files are the same when using cmp

當文件相同時, cmp file1 file2不執行任何操作。 那么如何在shell腳本中打印出相同的文件呢?

如果文件相同,則cpm的退出狀態為零,否則為非零。 因此,您可以使用類似的東西

cmp file1 file2 && echo "Files are identical"

如果要保存退出狀態,可以使用以下內容:

cmp file1 file2
status=$?
if [[ $status = 0 ]]; then
    echo "Files are the same"
else
    echo "Files are different"
fi

使用cmp的退出狀態代碼。 退出代碼為0表示它們是相同的:

$ cmp file1 file2; echo $?
0

在腳本中,您可以執行以下操作:

cmp file1 file2 && echo "same"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM