尝试“diff -s”

尝试“diff -s”

刚刚更新了代码……每次我都会在“else”序列中退出。您可以从我的服务器下载 update.sh 文件。它只包含 echo“Hallo update”

更新代码(2015.11.03)

    #/bin/bash
    updateoldmd5=`sed -n l  globalupdate.aix`
    updatenewmd5=`md5sum update.sh |cut -d ' ' -f 1`


    if [ $updateoldmd5 =  $updatenewmd5 ]

    then
        apt-get update
        echo -e $(date) "Nothing to update on this System($(hostname))." >> globalupdate.log
        wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh
        echo "Done"

    else
        chmod +x /root/update.sh
        ./root/update.sh
        echo -e $(date) "System ($(hostname)) Updated." >> globalupdate.log
        echo ""
        md5sum update.sh |cut -d ' ' -f 1 > globalupdate.aix
        echo "Update done"
        #Get new update.sh file for next update check of the node system.
        wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh

    fi

请注意。globalupdate.aix 文件仅包含以前的 update.sh 文件的 MDsum,用于与新下载的文件进行比较(以检查是否有任何更改应用于系统)。此脚本即将把相同的 update.sh 文件部署到许多 debian 服务器...

答案1

您也可以使用cmp。 从手册页 - cmp - compare two files byte by byte。 如果文件匹配,它将以 0 退出。

如果 cmp -s "$oldfile" "$newfile" ; 那么
   回显“没有任何改变”
别的
   echo “有些事情发生了变化”

答案2

保持简单。Diff 在有差异时返回 1,在无差异时返回 0。使用 if 语句。这是区分两个文件的方法

if diff file1 file2 > /dev/null
then
    echo "No difference"
else
    echo "Difference"
fi

为了解决你的问题(你正在比较两者之间的不同变量在上面的例子中使用这个(双等​​号是你缺少的)。

#/bin/bash
updateoldmd5=`sed -n l  globalupdate.aix`
updatenewmd5=`md5sum update.sh |cut -d ' ' -f 1`    
if [ "$updateoldmd5" == "$updatenewmd5" ]
then
    apt-get update
    echo -e $(date) "Nothing to update on this System($(hostname))." >> globalupdate.log
    wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh
    echo "Done"
else
    chmod +x /root/update.sh
    ./root/update.sh
    echo -e $(date) "System ($(hostname)) Updated." >> globalupdate.log
    echo ""
    md5sum update.sh |cut -d ' ' -f 1 > globalupdate.aix
    echo "Update done"
    #Get new update.sh file for next update check of the node system.
    wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh
fi

答案3

可能是您的第一个变量包含字符串形式的命令,而不是其返回值。我认为您忘记了变量 1 中命令周围的 `。

答案4

尝试“diff -s”

$ echo abc > file1

$ echo abc > file2

$ sha1sum file1 file2
03cfd743661f07975fa2f1220c5194cbaff48451 *file1
03cfd743661f07975fa2f1220c5194cbaff48451 *file2

$ diff file1 file2
# (No output.)

$ diff --report-identical-files file1 file2
Files file1 and file2 are identical

$ diff -s file1 file2
Files file1 and file2 are identical

相关内容