Mojoscream
Mojoscream

Reputation: 51

Multiple conditions in Bash for outputting error

My question is similar to ones asked multiple times before. I have a script that checks multiple variables on a machine and logs that data. For instance, I'm checking Firewall and Encryption on the machine, and if one or both of them are off I need to echo exactly what is turned off. I'm looking at using something along the lines of this...

if [ $firewall -eq "Off" ] ||  [ $Encryption -eq "Off" ]; then
echo Please fix ****
else
echo "Everything is great"
fi

I'm having a problem of how parse out the data in the second line.

EDIT: I've gotten some great answers, but the thing I'm curious about is, how would I work it if I have a list of 5 things I'm checking on, and say 2 of them are not meeting the standard. I'd like to output only those that are not passing to the end user so they know what they need to fix?

So given the example above, let's say I'm testing for Firewall, Encryption, Patches, and Disk Space. If the user needs to fix Firewall and Encryption, I'd like to output only those two. If another user has issues with Disk Space, one one to them, and another user has everything okay, then a message that everything is fine.

EDIT 2:* I'm pulling the information I require, the only problem is I need to have one of two different pop-up windows. Either one that shows what exactly is wrong, and that could be any number of things, or one that shows that everything is working properly.

So far I've come up with the code below, but it's showing me two windows, one that shows the errors, and the other "Everything is great" pop-up in that order. Where am I going wrong?

for i in Software Firewall XProtect; do
    if [[ "${!i}" == "Off" ]]; then 
        osascript -e 'tell app "System Events" to display dialog "Compliance Check results \r \r'$i' needs attention\r \rPress OK to continue."'
    Elseif
        osascript -e 'tell app "System Events" to display dialog "Compliance Check results \r \rEverything looks good. \r \rPress OK to continue."'
    fi
done

Thank you in advance for all the help.

Upvotes: 2

Views: 111

Answers (2)

michael501
michael501

Reputation: 1482

try this

if [[ ${firewall:-}  = "Off"  ||  ${Encryption:-} = "Off" ]]; then
   echo Please fix ****
else
   echo "Everything is great"
fi

Upvotes: 0

anubhava
anubhava

Reputation: 786101

-eq is used for Integer comparison only.

Better to simplify your code like this:

if [ "$firewall" = "Off" ]; then
   echo "firewall is Off"
elif  [ "$Encryption" = "Off" ]; then
   echo "Encryption is Off"
else
   echo "Everything is great"
fi

EDIT: To check multiple variable you can use this for loop:

for i in A B C D; do
    [[ "${!i}" == "Off" ]] && echo "$i is Off"
done

Upvotes: 2

Related Questions