0

I am trying to create a bash script to check and generate report on CIS Benchmark for RHEL7 v2.1.0. I am getting some issue while handling variable in 'if' condition. Here is my script:-

[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 ~]#

Both the output are same but still the check is failing.

2

I put declare -p RPUT OPUT before the if statement which printed out put:

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

Now that extra space was creating problem so I added a space in OPUT variable. It resolved the problem.

| improve this answer | |
  • Asking follow-up questions/requests in answers is not the right thing to do. Posting a new question is, but note that asking for software recommendations is off-topic here on Super User. I edited your answer. – Kamil Maciorowski Aug 12 '19 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.