Skip to main content
Question

API scripting issue - delete empty usergroups (and classes)


Forum|alt.badge.img+5

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

Forum|alt.badge.img+2
  • New Contributor
  • 10 replies
  • August 20, 2018

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


sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3532 replies
  • August 21, 2018

@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.


Forum|alt.badge.img+5
  • Author
  • Contributor
  • 15 replies
  • August 21, 2018

@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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3532 replies
  • August 21, 2018

@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.


Forum|alt.badge.img+5
  • Author
  • Contributor
  • 15 replies
  • August 21, 2018

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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3532 replies
  • August 21, 2018

@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?


Forum|alt.badge.img+5
  • Author
  • Contributor
  • 15 replies
  • August 21, 2018

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

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings