3

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

3 Answers 3

2

You could use

[[ $(spctl --status) == "assessments enabled" ]] \
    && (echo "Gatekeeper is enabled."; do-additional-stuff) \
    || (echo "Gatekeeper is disabled."; do-other-stuff)

which is technically a one-liner. But it's hard to read if you are not used to such things; and it gets even harder as soon as there are a lot of additional commands. Depending on your coding style etc something like

gk_is_enabled() {
    echo "Gatekeeper is enabled."
    # do lots of additional stuff
}

gk_is_disabled() {
    echo "Gatekeeper is disabled."
    # do lots of other stuff
}

[[ $(spctl --status) == "assessments enabled" ]] && gk_is_enabled || gk_is_disabled

might work then.

In addition you can shorten the [[ $(spctl --status) == "assessments enabled" ]] part (in any solution) to [[ $(spctl --status) == *enabled ]].


Also, if you want to go really crazy with showing off some bash skills you could do

assessments_enabled() {
    echo "Gatekeeper is enabled."
    # do lots of additional stuff
}

assessments_disabled() {
    echo "Gatekeeper is disabled."
    # do lots of other stuff
}

eval $(spctl --status | sed 's/ /_/')
4

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
3

This is one why that I might code it, depending on what other commands will need to be executed:

#!/bin/bash

if [[ $(spctl --status) == "assessments enabled" ]]; then
    echo "Gatekeeper is enabled."
    # Place additional commands after this comment.

else 
    echo "Gatekeeper is disabled."
    # Place additional commands after this comment.

fi
1
  • Nice, it already saved me one line of code. I am going to vote up and wait some other answers just to be sure it can be done differently and if not I will vote yours. Thanks user3439894
    – Fabio
    Commented Aug 1, 2019 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .