2个相等的字符串预期状态和当前状态评估为false

问题描述

我有一个** bash脚本,用于设置UFW ,其预期输出如下

user@host:~$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming),allow (outgoing),disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT IN    Anywhere                  
80/tcp                     ALLOW IN    Anywhere                  
443/tcp                    ALLOW IN    Anywhere                  
22/tcp (v6)                LIMIT IN    Anywhere (v6)             
80/tcp (v6)                ALLOW IN    Anywhere (v6)             
443/tcp (v6)               ALLOW IN    Anywhere (v6) 

我创建了一个测试功能以验证输出是否符合预期。我将输出手动复制到一个变量中,然后将输出存储在另一个变量中,这样我就可以比较两个2了:

function test() {
    # Verify that output is as excpeted
    local output="$(sudo ufw status verbose)"
    local expected="Status: active Logging: on (low) Default: deny (incoming),disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp LIMIT IN Anywhere 80/tcp ALLOW IN Anywhere 443/tcp ALLOW IN Anywhere 22/tcp (v6) LIMIT IN Anywhere (v6) 80/tcp (v6) ALLOW IN Anywhere (v6) 443/tcp (v6) ALLOW IN Anywhere (v6)"

    # echo
    # echo $output
    # echo
    # echo $expected
    # echo

    if [[ "$output" == "$expected" ]]; then
        echo "UFW setup: output was as expected"
    else
        echo "UFW setup: output NOT as expected!"
        echo "Please check UFW status before coninuing:"
        echo
        sudo ufw status verbose
    fi

输出

luc@E580-debian:~$ test
UFW setup: output NOT as expected!
Please check UFW status before coninuing:

Status: active
Logging: on (low)
Default: deny (incoming),disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT IN    Anywhere                  
80/tcp                     ALLOW IN    Anywhere                  
443/tcp                    ALLOW IN    Anywhere                  
22/tcp (v6)                LIMIT IN    Anywhere (v6)             
80/tcp (v6)                ALLOW IN    Anywhere (v6)             
443/tcp (v6)               ALLOW IN    Anywhere (v6)             

据我所知,它们是完全相同的,但我也不知道是否有办法检查是否有隐藏的字符引起这种情况。或者,也许有一种方法可以删除/替换它们。

解决方法

您正在比较困难的方法。您对UFW输出中的/ tabs / spaces一无所知。这将是更专业的方法。请根据您的需求进行调整:

vim ./check_ufw.sh

#!/usr/bin/env bash
#:: Author: pr0logas
#:: Date: 2020-08-20
#:: Description: This program checks if the UFW firewall rules have not changed.

expected_output_file='/tmp/ufw_original.txt'
current_output_file='/tmp/ufw_current.txt'

echo "Just pushing some text as original UFW input to make FAILURE" > $expected_output_file

function check_diff() {
        ufw status numbered verbose > $current_output_file
        diff -q $expected_output_file $current_output_file 1> /dev/null

        if [[ $?  == "0" ]]; then
                echo "SUCCESS! UFW rules are the same."
        else
                echo "FAIL! UFW rules differ,attention needed!"
                exit 1
        fi
}

check_diff

为100%确定所需的输出,我建议您像这样第一次将当前输出复制为原始输入:

cat /tmp/ufw_current.txt > /tmp/ufw_original.txt

然后发表评论:

#echo "Just pushing some text as original UFW input to make FAILURE" > $expected_output_file