.sh Script Runs from Terminal, But Throws an Error From Jamf .... Ideas?

scalar-its
New Contributor II

Hi there,

I have a shell script that uninstalls a specific program -- it kills the process if the app is running, looks in three different directories for anything related to the file and deletes them.

When ran from terminal (if the .sh is on the users desktop for example), the script runs completely fine and removes the app as i'd expect it to.

When i add it into Jamf, and launch the Policy I get an error:
Script exit code: 2
Line 55: Syntax error near unexpected token 'fi'
Line 55: 'fi'

Not too sure how the fi on line 55 is wrong, especially if the script runs without error when manually run. I've ensured that I have the Script Content in Jamf set to "Shell" as well... Any suggestions?

Heres the script (Line 55 is the very last line, closing the initial IF statement):

#!/bin/sh

PUBLIC_THUMBPRINT="$1"

if [ $# -eq 0 ]; then
    echo "Searching for installed Control clients..."
    PUBLIC_THUMBPRINTS=$(ls /opt/ | grep -o -w -E "connectwisecontrol-[[:alnum:]]{16}" | grep -Eo ".{16}$")
    PUBLIC_THUMBPRINTS_ARR=($PUBLIC_THUMBPRINTS)

    if [ ${#PUBLIC_THUMBPRINTS} -eq 0 ] ; then
        echo
        echo "No Control clients found!"
        echo "Terminating cleanup"
        exit
    else
        echo "Found client(s) with the following public thumbprint(s):"
        echo
        echo "$PUBLIC_THUMBPRINTS"
        echo "Beginning cleanup..."
        fi

        for thumbprintKey in "${!PUBLIC_THUMBPRINTS_ARR[@]}" ; do
            THUMBPRINT="${PUBLIC_THUMBPRINTS_ARR[$thumbprintKey]}"
            echo "Unloading client launch agents ($THUMBPRINT)..."

            NAMES_OF_USERS_STR2=$(ps aux | grep $THUMBPRINT | grep -Eo '^[^ ]+')
            NAMES_OF_USERS_ARR2=($NAMES_OF_USERS_STR2)

            for key2 in "${!NAMES_OF_USERS_ARR2[@]}" ; do
                POTENTIAL_USER2="${NAMES_OF_USERS_ARR2[$key2]}"
                if [ $POTENTIAL_USER2 != "root" ] ; then
                    NON_ROOT_USER_ID2=$(id -u $POTENTIAL_USER2)
                    echo "Unloading client launch agent for user $POTENTIAL_USER2"
                    launchctl asuser $NON_ROOT_USER_ID2 launchctl unload /Library/LaunchAgents/connectwisecontrol-$THUMBPRINT-onlogin.plist >/dev/null 2>&1
                fi
            done

            echo "Unloading client launch daemon ($THUMBPRINT)..."
            launchctl unload "/Library/LaunchDaemons/connectwisecontrol-$THUMBPRINT.plist" >/dev/null 2>&1

            echo "Deleting client launch agents ($THUMBPRINT)..."
            rm "/Library/LaunchAgents/connectwisecontrol-$THUMBPRINT-onlogin.plist" >/dev/null 2>&1
            rm "/Library/LaunchAgents/connectwisecontrol-$THUMBPRINT-prelogin.plist" >/dev/null 2>&1

            echo "Deleting client launch daemon ($THUMBPRINT)..."
            rm "/Library/LaunchDaemons/connectwisecontrol-$THUMBPRINT.plist" >/dev/null 2>&1

            echo "Deleting client installation directory ($THUMBPRINT)..."
            rm -rf "/opt/connectwisecontrol-$THUMBPRINT.app/" >/dev/null 2>&1                   
        done

        echo "Cleanup complete!"
        exit
    fi
fi
1 ACCEPTED SOLUTION

KrisMallory
New Contributor III

Do you need the container:

if [ $# -eq 0 ]; then
(and the last fi to close it out)

I'm not sure what it's purpose is. If you put in an else for the container with an echo, I would imagine you'd see that triggered. Or if you removed the container, it looks like would the script still run the way you intend it.

View solution in original post

9 REPLIES 9

Hugonaut
Valued Contributor II

i see 3 ifs & 4 fis

line 10 is ended line 20

line 31 is ended line 35

line 5 is ended line 54

line 55 is a floating fi?

*SideNote - start using #!/bin/bash to path .sh , in catalina scripts pathed #!/bin/sh will path to .zsh

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

mm2270
Legendary Contributor III

Good catch on the extra fis @Hugonaut. But it seems to me the extraneous fi is the one on line 20, not on line 55. Line 10 starts a large if/then/else section that terminates (exits) if [ ${#PUBLIC_THUMBPRINTS} -eq 0 ] or continues in the else section and does the lions share of the work in the script, including the for thumbprintKey in loop. That whole section should end with the fi on line 54, and the entirety of the script on line 55. So if you get rid of the fi on line 20 I believe that would fix it. Not that I have actually tested the script at all. I'm only going by what I can see.

Hugonaut
Valued Contributor II

@mm2270 i was contemplating the fi to call out, i never ran a test so i assumed 55 since that threw the error so thanks for clarifying all that for @scalar-its

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

scalar-its
New Contributor II

@Hugonaut @mm2270 Wow, I can't believe I missed that.. Thank you guys for helping out. A little embarrassed haha.

So i did remove the "fi" from line 20 as suggested, and sure enough the script did run successfully -- apparently..

It does get an exit code of 0, but the application doesn't uninstall.

If I run the script as mentioned before from the Desktop, it does uninstall successfully. The script is exactly the same on Jamf, as it is on my Desktop.

Here is the output after Jamf runs the policy:

Executing Policy Script - Remove ScreenConnect Remote Agent Running script ScreenConnect Remote Agent Removal... Script exit code: 0 Script result: Checking for patches... No patch policies were found. Running Recon... Retrieving inventory preferences from https://scalar.jamfcloud.com/... Finding extension attributes... Locating package receipts... Locating accounts...

So I'm still a little confused here.. Any other ideas why the application might not be uninstalling if the Exit Code = 0 ? Is there a log I can check?

Thank you!

merps
Contributor III

You could try adding verbose output as described in this Jamf KB Article to help with debugging. Make sure to have a carriage return followed by a blank like as described in the comments of the article.

#!/bin/bash -v
exec 2>&1

## script logic starts below this line

If I had to guess, I'd say the outer if isn't evaluating to true, so the script just ends.

if [ $# -eq 0 ]; then

The other thing could be that Jamf script parameters start with $4, and you're reading $1 for the PUBLIC_THUMBPRINT.

scalar-its
New Contributor II

@merps Hey there, thanks for the suggestions!

Well whats odd though, is that the outer IF does evaluate to True when I run the script manually from terminal instead of allowing Jamf to push its Script Policy..

I'll try the suggestions you had for me, and post back - thank you!

scalar-its
New Contributor II

Hmm, no luck.. here is what the Jamf Log shows after making the edits to my script:

Executing Policy Script - Remove ScreenConnect Remote Agent Running script ScreenConnect Remote Agent Removal... Script exit code: 0 Script result: #!/bin/bash -v<br/>exec 2>&1<br/><br/>PUBLIC_THUMBPRINT="$1"<br/><br/>if [ $# -eq 0 ]; then<br/> echo "Searching for installed Control clients..."<br/> PUBLIC_THUMBPRINTS=$(ls /opt/ | grep -o -w -E "connectwisecontrol-[[:alnum:]]{16}" | grep -Eo ".{16}$")<br/> PUBLIC_THUMBPRINTS_ARR=($PUBLIC_THUMBPRINTS)<br/><br/> if [ ${#PUBLIC_THUMBPRINTS} -eq 0 ] ; then<br/> echo<br/> echo "No Control clients found!"<br/> echo "Terminating cleanup"<br/> exit<br/> else<br/> echo "Found client(s) with the following public thumbprint(s):"<br/> echo<br/> echo "$PUBLIC_THUMBPRINTS"<br/> echo "Beginning cleanup..."<br/><br/> for thumbprintKey in "${!PUBLIC_THUMBPRINTS_ARR[@]}" ; do<br/> THUMBPRINT="${PUBLIC_THUMBPRINTS_ARR[$thumbprintKey]}"<br/> echo "Unloading client launch agents ($THUMBPRINT)..."<br/><br/> NAMES_OF_USERS_STR2=$(ps aux | grep $THUMBPRINT | grep -Eo '^[^ ]+')<br/> NAMES_OF_USERS_ARR2=($NAMES_OF_USERS_STR2)<br/><br/> for key2 in "${!NAMES_OF_USERS_ARR2[@]}" ; do<br/> POTENTIAL_USER2="${NAMES_OF_USERS_ARR2[$key2]}"<br/> if [ $POTENTIAL_USER2 != "root" ] ; then<br/> NON_ROOT_USER_ID2=$(id -u $POTENTIAL_USER2)<br/> echo "Unloading client launch agent for user $POTENTIAL_USER2"<br/> launchctl asuser $NON_ROOT_USER_ID2 launchctl unload /Library/LaunchAgents/connectwisecontrol-$THUMBPRINT-onlogin.plist >/dev/null 2>&1<br/> fi<br/> done<br/><br/> echo "Unloading client launch daemon ($THUMBPRINT)..."<br/> launchctl unload "/Library/LaunchDaemons/connectwisecontrol-$THUMBPRINT.plist" >/dev/null 2>&1<br/><br/> echo "Deleting client launch agents ($THUMBPRINT)..."<br/> rm "/Library/LaunchAgents/connectwisecontrol-$THUMBPRINT-onlogin.plist" >/dev/null 2>&1<br/> rm "/Library/LaunchAgents/connectwisecontrol-$THUMBPRINT-prelogin.plist" >/dev/null 2>&1<br/><br/> echo "Deleting client launch daemon ($THUMBPRINT)..."<br/> rm "/Library/LaunchDaemons/connectwisecontrol-$THUMBPRINT.plist" >/dev/null 2>&1<br/><br/> echo "Deleting client installation directory ($THUMBPRINT)..."<br/> rm -rf "/opt/connectwisecontrol-$THUMBPRINT.app/" >/dev/null 2>&1 <br/> done<br/><br/> echo "Cleanup complete!"<br/> exit<br/> fi <br/>fi<br/> Running Recon... Retrieving inventory preferences from https://scalar.jamfcloud.com/... Locating package receipts... Locating accounts... Searching path: /opt Locating software updates... Locating printers... Searching path: /Applications Locating hardware information (Mac OS X 10.14.6)... Gathering application usage information...

Sorry for picking everyone's brain, but are there any other ideas? This one is stumping me.

Thank you!

KrisMallory
New Contributor III

Do you need the container:

if [ $# -eq 0 ]; then
(and the last fi to close it out)

I'm not sure what it's purpose is. If you put in an else for the container with an echo, I would imagine you'd see that triggered. Or if you removed the container, it looks like would the script still run the way you intend it.

scalar-its
New Contributor II

@KrisMallory

Yup, that was it!! I just removed the if [ $# -eq 0 ]; then container completely, and removed the last fi on Line 55 and tested - uninstalled my program right away!

I can finally breathe!

Thank you!