String 2个相等的字符串(预期状态和当前状态)计算为false

String 2个相等的字符串(预期状态和当前状态)计算为false,string,bash,output,verify,ufw,String,Bash,Output,Verify,Ufw,我有一个**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 --

我有一个**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), 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)"

    # 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), 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)             

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

您正在比较困难的方法。您对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

空格可能有所不同。在脚本中放入
set-x
以查看跟踪。养成不使用
test
作为函数名的习惯test,这可能会导致混淆。如果
sudo ufw status verbose
调用生成多行响应,则
输出
变量将包含嵌入式换行符(
\n
);不确定
$output
(包括嵌入式
\n
s)如何与
$expected
匹配,后者至少不包括任何
\n
s???比较
echo“${output}”|od-c
echo“${expected}”|od-c
的输出以查看差异您使用
echo$output
查看变量包含的内容,但是,您被误导以为输出都在一行上。我建议您将多行输出与
diff
进行比较,而不是字符串比较,因为这将使您现在和以后更容易发现差异。当您上载修复程序时,我根据注释更改了一些内容,并使其以几乎相同的方式工作:PThanks反正是为了响应,我希望舒尔在未来的脚本编写中采纳您的建议!