If I write a script using an if statement, and use "then exit 0", what would I enter after "else" to make the script continue running if the result of the if statement is a different value? I thought I knew how to do this but I can't think of it.
Question with if statement and using exit zero
Best answer by jtrant
The script is an extension attribute. I'm pasting it below. I use this same EA for more than one agent. I just change the file paths and process names. The EA works, but what I want is for the EA to stop running if the app/agent is not actually installed and simply report that it is not installed. I was thinking that if I add "exit 0" after the EA reports that the app is not installed, the EA can simply report that it's not installed and stop evaluating the agent running or its version. I know this is possible. I just don't know how to structure it correctly. Here's a screenshot of what the EAs look like when the app or agent is installed, running and its version.
.
#!/bin/zsh
# Is CrowdStrke Falcon app installed?
if [[ -d /Applications/Falcon.app ]]; then
Installed="Installed: Yes"
else
Installed="Installed: No"
fi
# Check if CrowdStrike agent is running
proc=$(ps aux | grep "com.crowdstrike.falcon.Agent" | /usr/bin/awk '{print $2}')
if [ -z "$proc" ]; then
procRunning="Process Running: No"
else
procRunning="Process Running: Yes"
fi
# Check CrowdStrike version
Version=$(defaults read /Applications/Falcon.app/Contents/Info.plist CFBundleShortVersionString)
# Results
echo "<result>$(printf '%s\\n' "$Installed" "$procRunning" "Version: $Version")</result>"
Would something like this work:
#!/bin/zsh
# Is CrowdStrke Falcon app installed?
if [[ -d /Applications/Falcon.app ]]; then
Installed="Installed: Yes"
# Check if CrowdStrike agent is running
proc=$(ps aux | grep "com.crowdstrike.falcon.Agent" | /usr/bin/awk '{print $2}')
if [ -z "$proc" ]; then
procRunning="Process Running: No"
else
procRunning="Process Running: Yes"
# Check CrowdStrike version
Version=$(defaults read /Applications/Falcon.app/Contents/Info.plist CFBundleShortVersionString)
fi
echo "<result>$(printf '%s\\n' "$Installed" "$procRunning" "Version: $Version")</result>"
else
echo "<result>Installed: No</result"
fi
# Results
Nesting your checks only if the initial if statement is true should work, and I've done something similar in the past.
Having said that, you might find multiple EAs for each validation more manageable in the long term, especially when trying to create smart groups around certain attributes or scoping criteria based on the EA results.
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.