if 子句中的变量问题

if 子句中的变量问题

我正在尝试创建一个 bash 脚本来检查并生成 RHEL7 v2.1.0 的 CIS Benchmark 报告。在处理“if”条件下的变量时,我遇到了一些问题。这是我的脚本:-

[root@test ~]# cat test.sh
#!/bin/bash
echo $(date) > ./report
echo $(date) > ./passed
echo $(date) > ./failed

echo -e "\n1.1.1.1\t Ensure mounting of cramfs filesystems is disabled" >> ./report
PROB='1.1.1.1'
CMD='modprobe -n -v cramfs'
RPUT="`$CMD`"
OPUT="install /bin/true"
if [ "$RPUT" == "$OPUT" ]
then
        echo -e "\nPASSED" >> ./report
        echo -e "$PROB" >> ./passed
else
        echo -e "\nFAILED" >> ./report
        echo -e "$PROB" >> ./failed
fi
echo -e "Your Output:\n $RPUT \nRequired Output:\n $OPUT " >> ./report
[root@test ~]#
[root@test ~]# ./test.sh
[root@test ~]#
[root@test ~]# cat report
Mon Aug 12 11:56:19 IST 2019

1.1.1.1  Ensure mounting of cramfs filesystems is disabled

FAILED
Your Output:
 install /bin/true
Required Output:
 install /bin/true
[root@test ~]#

两个输出相同,但检查仍然失败。

答案1

我 在打印出来的语句declare -p RPUT OPUT前面放了:if

declare -- RPUT="install /bin/true "
declare -- OPUT="install /bin/true"

现在额外的空间造成了问题,所以我在OPUT变量中添加了一个空格。它解决了这个问题。

相关内容