Mac OS X - Bash script if statement based on system status Creating a bash script and using the if statement, how do I execute a command based on a system status?

if a specific system status is enabled do something, else do something different?

I give you a real example.  Let's say, for example,  I want to execute some commands based on the Mac OS X Gatekeeper status of the system.

Example: the following command line on your Mac OS X spctl --status will give you 2 possible output: assessments disabled or assessments enabled.

Can I do something like that below? Or there is a cleaner and more efficient way to do it? Thank you

#!/usr/bin/env bash

gatekeeper_status=$(spctl --status)

if [[ ${gatekeeper_status} ==  "assessments enabled" ]]
then
    echo "Gatekeeper is enabled do some stuff"
else
    echo "Gatekeeper is disabled do some stuff"
fi
Your example is clear, concise and maintainable but lacks any error checking. This example uses a case statement to parse the status and will exit if neither string is in the variable  -> 

status=$(spctl --status)

case $status in
        'assessments enabled') echo "Gatekeeper is enabled."
        #do more stuff
        #and more stuff
        ;;

        'assessments disabled') echo "Gatekeeper is disabled."
        #do more stuff
        #and more stuff
        ;;

        *) echo "An error has occurred- Exiting script"
        exit
        ;;
esac


It could also be written like this ->

if   [[ $status == "assessments enabled" ]]
then 
     echo "Gatekeeper is enabled."
     #do more stuff
elif [[  $status == "assessments disabled" ]]
then 
     echo "Gatekeeper is disabled"
     #do more stuff
else
     echo "An error has occurred- Exiting script"
     exit
fi