Need help with a script

DirkM2012
Contributor

Hello together,

I'm pretty new to scripting on OS X and hope that one of you can help me with this.

I have a script that is supposed to check if a certain app is installed, get its version and do something if it's not yet installed.

#!/bin/sh
echo "Checking installed version"
minVer=“2.1.0”
installedVer=`system_profiler SPApplicationsDataType | grep -B7 "McAfee Endpoint Protection for Mac" | awk '/Version/ { print $2 }'`
if [ -z "$installedVer" ]
then
  echo "McAfee Endpoint Protection for Mac is not installed"
else
  echo "Installed version is $installedVer"
  if [ “$installedVer” == “$minVer” ]
  then
    echo "Installed version $installedVer is up-to-date"
    exit 1
  fi
fi
echo "Installing Mcafee Endpoint Protection 2.1.0"
exit 1

The script works fine when I run in in Terminal but when I execute it via Casper Remote or policy I get the following:

Sending Wake On LAN command... Opening SSH Connection to 192.168.1.15... Authenticating... Successfully authenticated. Verifying Computer's Identity... The MAC Address has been verified. Checking Operating System Version... Running Mac OS X 10.9.1 (13B42) Verifying /usr/sbin/jamf... /usr/sbin/jamf is current (9.23) Verifying /Library/Preferences/com.jamfsoftware.jamf.plist... Preparing Policy... Executing Policy 2014-02-16 at 9:24 AM | CasperAdmin| 1 Computer... Creating directory structure for /Library/Application Support/JAMF/Downloads/ Downloading http://CasperServer/CasperShare/Scripts/test.sh... Running script test.sh... Script exit code: 1 Script result: Checking installed version Installed version is 2.1.0 Installing Mcafee Endpoint Protection 2.1.0 Submitting log to https://jssserver:8443/ Finished.

As you can see, while it detects that version 2.1.0 is already installed, it still tries to install it again.

Can someone tell me what I'm doing wrong?

Thanks,
Dirk

1 ACCEPTED SOLUTION

bentoms
Release Candidate Programs Tester

@dmatth01, the echo statement "Installing McAfee" is outside your IF statement.

So will echo anyway.

You'll need to add the not installed action, to the part of then IF statement where you advise it's not installed.

BUT. Why script it at all? You could scope a smart group to having this app installed & if not then install.

View solution in original post

11 REPLIES 11

bentoms
Release Candidate Programs Tester

@dmatth01, the echo statement "Installing McAfee" is outside your IF statement.

So will echo anyway.

You'll need to add the not installed action, to the part of then IF statement where you advise it's not installed.

BUT. Why script it at all? You could scope a smart group to having this app installed & if not then install.

DirkM2012
Contributor

@bentoms, thanks for the quick reply.

I haven't touched smart groups yet, still trying to figure out some basics :-)

Shouldn't this exit terminate the script early without going all the way to the end? Or is exit just a way to set the return code to success or failure?

echo "Installed version $installedVer is up-to-date"
    exit 1

bentoms
Release Candidate Programs Tester

@dmatth01, I think it's just exiting the IF. Might be as it's a nested IF. Not sure off the top of my head.

BUT, you're really duplicating functionality you've paid for.

Have a look into Licensed Software & Smart Groups, then let the JSS do the heavy lifting.

mm2270
Legendary Contributor III

I'd have to agrees with @bentoms 100% on this. Make use of Smart Groups to gather any Macs that either a) don't have the application installed, or b) have a version other than what you're deploying installed. Then use that as the scope for the policy and you won't need to worry about scripting anything. The Casper Suite already captures all this information so use it to your advantage.
In some cases if the item you're looking to get version information for isn't captured by default, say because its not actually in the /Applications/ path, then the next best thing would be to create an Extension Attribute to capture that version string, then build your Smart Group using that criteria.

Also, I'm taking a guess here since you didn't explain exactly how you're using this, but if you're using your script set up as "Before" as part of a policy that has the McAfee package in it, even if you exit 1 in your script, to my knowledge this won't actually stop the rest of the policy from continuing, which is likely why you're seeing the installation happen in the policy log. Having a script set up as "Before" in a policy isn't treated exactly the same as having a preinstall or preflight script embedded within a package.

DirkM2012
Contributor

Thanks everybody. Changing the if statement and moving the "installing" section properly solved my issue. I will also look into smart groups. I wish there would be a Casper Guide for Dummies and Windows admins with more detailed real world examples but eventually I will figure this out.

bentoms
Release Candidate Programs Tester

@dmatth01.

Glad it helped.

Did you do a JumpStart or did you inherit the environment?

TBH, the answer to the above shouldn't matter. But JAMF Support would be the best to help. You should be able to schedule a WebEx to explain Smart groups etc.

Also, please "mark as answer" the posts that helped.

DirkM2012
Contributor

My JumpStart was in 2012 and I look into Casper about twice a year (my main job is to manage about 4000 Windows clients all around the world). We do Macs more as a hobby (means nobody will commit to officially supporting them) and after the initial setup (make sure they get tracked, Apple updates get installed automatically and offer some standard software via Self Service app) it was basically left alone. I usually try to find my answers via Google (which works quiet well for Windows and Winbatch) and I still have to get used to the new world of OS X and Casper.

bentoms
Release Candidate Programs Tester

@dmatth01, I know what you mean. I have other products that I support that I only admin infrequently.

TBH, in your situation I'd reach out to JAMFSupport when you need to look at the JSS.

They should be able to advise on the best way forward & over a webex assist further.

Some basics such as smart groups can really save you time. Negating the need to spend time testing scripts & researching solutions.

Your suggest if some more basic guides is a good one, & I'd suggest you raise a feature request too.

DirkM2012
Contributor

I just looked at licenses and smart groups, set up a license for McAfee EPM 2.1.0, a new smart group for containing all computers that are missing EPM and a new policy to push EPM ongoing to all those computers that are missing EPM. Seems to be working pretty good so far. No scripting other than creating the base installer that I already used for the Self Service app. Thanks for the great tip!

bentoms
Release Candidate Programs Tester

Awesome!

It's a really powerful function. Enjoy!

sean
Valued Contributor

'exit' will exit the script or subshell at that point with the value set

As reference, you are setting an exit value of 1 when the script has been successful. Consider using 'exit 0' for success as this is the convention.