Question with if statement and using exit zero

howie_isaacks
Valued Contributor II

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. 

1 ACCEPTED SOLUTION

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.

View solution in original post

8 REPLIES 8

PhillyPhoto
Valued Contributor

Can you post your script? It should only exit if the condition is satisfied or exit the if function and continue on if not.

howie_isaacks
Valued Contributor II

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.

.Screenshot 2023-07-18 at 13.58.04.png

 

 

#!/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.

PhillyPhoto
Valued Contributor

This is the way.jpeg

howie_isaacks
Valued Contributor II

Thanks! I think I over complicated it as usual. I totally forgot about nesting. I just put this in my Jamf Pro server and ran an inventory of my own Mac. I have CrowdStrike installed, so it shows that it's installed, the process running, and the version. When we see a Mac without CrowdStrike installed is the test. We only want to see the result that it's not installed. Obviously it won't be running and there won't be a version. We are viewing all of this in Power BI, not just in Jamf Pro. We needed this to be formatted in a way that it looked good in Power BI.

Yes, this should meet your requirements. In my tests, a Mac without CS installed returns "Installed: No" and nothing else.

If you want to keep your format, you can make a conditional check on the version:

# Check CrowdStrike version
if [[ -d /Applications/Falcon.app ]]; then
    Version=$(defaults read /Applications/Falcon.app/Contents/Info.plist CFBundleShortVersionString)
else
    Version="x.x"
fi

Then if you see "x.x" you know it's not installed.

To take it one step further, you can get rid of the "else" checks altogether too:

#!/bin/zsh

# Set default values:
Installed="No"
procRunning="Yes"
Version="x.x"

# Is CrowdStrke Falcon app installed?
if [[ -d /Applications/Falcon.app ]]; then
    Installed="Yes"
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="No"
fi

# Check CrowdStrike version
if [[ -d /Applications/Falcon.app ]]; then
    Version=$(defaults read /Applications/Falcon.app/Contents/Info.plist CFBundleShortVersionString)
fi

# Results
echo "<result>$(printf '%s\n' "Installed: $Installed" "Process Running: $procRunning" "Version: $Version")</result>"

This got even more complicated when I had to write another EA that dealt with an app that had two different install paths. The newer version installs into /Applications. The old one installed into /Library. For that one I had to account for both locations so that "installed" would fit either install path. The rest of your suggestion was helpful. It didn't occur to me to nest if statements like that.