API scripting issue - delete empty usergroups (and classes)

MDMMan
New Contributor III

Novice scripter trying to modify a script from github to delete all empty usergroups from Jamf. Currently getting line 26: syntax error near u'expected token `do
Eventually I want this script to also delete empty classes as well.

#!/bin/bash 
# Script to delete empty usergroups
#
jssUser="******" # Full JSS Admin Account
jssPass="******" # Password for the JSS Admin Account
index="0"
usergroups=()
echo $jssURL
IDs=`curl -sk -u $jssUser:$jssPass -H "Accept: text/xml" "https://******.jamfcloud.com/JSSResource/usergroups" -X GET`
#
#
size=`echo $IDs | xpath //user_groups/size | sed 's/<[^>]*>//g'`
#
#
echo $size " user groups will be scanned."
#
#Put the IDs into an array
while [ $index -lt ${size} ] 
do  
    index=$[$index+1]
    usergroups+=(`echo $IDs | xpath //user_groups/user_group[${index}]/id | sed 's/<[^>]*>//g'`)
    done
#
# Sort through each user ID individually and grep for IDs size
for i in "${usergroups[@]}"
do
    usergroup=`curl -sk -u $jssUser:$jssPass  -H "Accept: text/xml" "https://******.jamfcloud.com/JSSResource/usergroups/id/${i}" -X GET`
    groupsize=`echo $usergroup | xpath //user_group/users/size | grep id`
#
    # Delete users that meet our criteria
    if [[$groupsize -eq 0]] ; then
        echo "Deleting ${i}"
        curl -k -v -u $jssUser:$jssPass  -H "Accept: text/xml" "https://******.jamfcloud.com/JSSResource/usergroups/id/${i}" -X DELETE
    fi
done
7 REPLIES 7

connor
New Contributor III

Are you missing a second done to close off the if statements? I'd give that a go!

sdagley
Esteemed Contributor II

@MDMMan When posting scripts to Jamf Nation, please use leading and trailing triple backticks (```) to signify a code block.

As a handy tool for checking scripts, take a look at ShellCheck. It gives you interactive feedback on your script, so you could quickly try @connor's suggestion to see if that eliminated the syntax error.

MDMMan
New Contributor III

@connor I have two DOs and two DONEs. I have one IF and one FI.

@sdagley Thanks for the backticks tip. It definitely makes the code block more readable. ShellCheck says what Connor said. I tried adding another done but it didn't make a difference. ShellCheck doesn't seem to be able to read the done for either do. It also doesn't like the While statement.

sdagley
Esteemed Contributor II

@MDMMan You need a space after the [[ and before the ]] on line 31 in the script as it currently reads above. Once you do that ShellCheck will have several additional suggestions.

MDMMan
New Contributor III

Thanks. I have reworked the script based on shellcheck. Still getting errors. line 22: syntax error near unexpected token `do
It wants me to add additional double quotes that do not appear to be needed..

#!/bin/bash 
# Script to delete empty usergroups
#
jssUser="****" # Full JSS Admin Account
jssPass="****" # Password for the JSS Admin Account
index="0"
usergroups=()
IDs=$(curl -sk -u $jssUser:$jssPass -H "Accept: text/xml" "https://****.jamfcloud.com/JSSResource/usergroups" -X GET)
#
size=$(echo $IDs | xpath //user_groups/size | sed 's/<[^>]*>//g')
#
#Put the IDs into an array
while [ $index -lt $size ] 
do  
    index=$($index+1)
    usergroups+=( "$(echo "$IDs | xpath //user_groups/user_group[${index}]/id | sed 's/<[^>]*>//g')")" )
    echo $usergroups >>"/tmp/usergroups.txt"
    done
#
# Sort through each user ID individually and grep for IDs size
for i in "${usergroups[@]}"
do
    usergroup=$(curl -sk -u $jssUser:$jssPass  -H "Accept: text/xml" "https://****.jamfcloud.com/JSSResource/usergroups/id/${i}" -X GET)
    groupsize="$(echo "$usergroup | xpath //user_group/users/size | grep id")"
#
    # Delete users that meet our criteria
    if [[ $groupsize -eq 0 ]] ; then
        echo "Deleting ${i}"
        curl -k -v -u $jssUser:$jssPass  -H "Accept: text/xml" "https://****.jamfcloud.com/JSSResource/usergroups/id/${i}" -X DELETE
    fi
done

sdagley
Esteemed Contributor II

@MDMMan ShellCheck isn't showing a syntax error for the most recent version of the script you've posted. What did you change from the version you're trying to run?

MDMMan
New Contributor III

Here is the current version that is still returning the syntax error when running the script via terminal. line 22: syntax error near unexpected token `do

#!/bin/bash 
# Script to delete empty usergroups
#
jssUser="****" # Full JSS Admin Account
jssPass="*****" # Password for the JSS Admin Account
index="0"
usergroups=()
IDs=$(curl -sk -u $jssUser:$jssPass -H "Accept: text/xml" "https://****.jamfcloud.com/JSSResource/usergroups" -X GET)
#
size=$(echo $IDs | xpath //user_groups/size | sed 's/<[^>]*>//g')
#
#Put the IDs into an array
while [ $index -lt $size ] 
do  
    index=$($index+1)
    usergroups+=( "$IDs | xpath //user_groups/user_group[${index}]/id | sed 's/<[^>]*>//g')" )
    echo $usergroups >>"/tmp/usergroups.txt"
    done
#
# Sort through each user ID individually and grep for IDs size
for i in "${usergroups[@]}"
do
    usergroup=$(curl -sk -u $jssUser:$jssPass  -H "Accept: text/xml" "https://*****.jamfcloud.com/JSSResource/usergroups/id/${i}" -X GET)
    groupsize="$usergroup | xpath //user_group/users/size | grep id"
#
    # Delete users that meet our criteria
    if [[ $groupsize -eq 0 ]] ; then
        echo "Deleting ${i}"
        curl -k -v -u $jssUser:$jssPass  -H "Accept: text/xml" "https://****.jamfcloud.com/JSSResource/usergroups/id/${i}" -X DELETE
    fi
done