Refreshing Application Folder

agetz
Contributor

I am working on a script that will prep the Application folder for another user. This will remove all the user installed apps. I have been working on this for a bit and am having trouble finding my mistake. Im sure there are there are other ways to do this better and I am all ears. The issue here is that my array seems to not be splitting correctly at the end, causing my end result to be one line instead of multiple looped entries. Thanks for any assistance.

#!/bin/bash

IFS=","

# create an array with all the filer/dir inside ~/myDir
array=(/Applications/*)

delete=("/Applications/Utilities","/Applications/App Store.app","/Applications/Automator.app","/Applications/Calculator.app","/Applications/Calendar.app","/Applications/Chess.app","/Applications/Contacts.app","/Applications/DVD Player.app","/Applications/Dashboard.app","/Applications/Dictionary.app","/Applications/FaceTime.app","/Applications/Font Book.app","/Applications/Image Capture.app","/Applications/Launchpad.app","/Applications/Mail.app","/Applications/Maps.app","/Applications/Messages.app","/Applications/Mission Control.app","/Applications/Notes.app","/Applications/Photos.app","/Applications/Photo Booth.app","/Applications/Preview.app","/Applications/QuickTime Player.app","/Applications/Reminders.app","/Applications/Safari.app","/Applications/Self Service.app","/Applications/Siri.app","/Applications/Stickies.app","/Applications/System Preferences.app","/Applications/TextEdit.app","/Applications/Time Machine.app","/Applications/GarageBand.app","/Applications/Pages.app","/Applications/Numbers.app","/Applications/Keynote.app","/Applications/iTunes.app","/Applications/iBooks.app","/Applications/QuestarStudent.app","/Applications/NWEA Secure Testing Browser.app","/Applications/LockDown Browser.app","/Applications/Google Chrome.app")



for del in ${delete[@]}
do
   array=("${array[@]/$del}") #Quotes when working with strings
done


#for i in "${!array[@]}"; do
 #   new_array+=( "${array[i]}" )
#done
#array=("${new_array[@]}")
#unset new_array



# iterate through array using a counter
for ((i=0; i<${#array[@]}; i++)); do

    echo "Removing: $i ${array[$i]}"

#    rm -rf "${array[$i]}"


done
1 ACCEPTED SOLUTION

agetz
Contributor

Revised again after having issues with files with space. Quotes needed to be around the "${delete[@]}"

#!/bin/bash

# create an array with all the filer/dir inside ~/myDir
array=(/Applications/*)

delete=("/Applications/Utilities"
"/Applications/App Store.app"
"/Applications/Automator.app"
"/Applications/Calculator.app"
"/Applications/Calendar.app"
"/Applications/Chess.app"
"/Applications/Contacts.app"
"/Applications/DVD Player.app"
"/Applications/Dashboard.app"
"/Applications/Dictionary.app"
"/Applications/FaceTime.app"
"/Applications/Font Book.app"
"/Applications/Image Capture.app"
"/Applications/Launchpad.app"
"/Applications/Mail.app"
"/Applications/Maps.app"
"/Applications/Messages.app"
"/Applications/Mission Control.app"
"/Applications/Notes.app"
"/Applications/Photos.app"
"/Applications/Photo Booth.app"
"/Applications/Preview.app"
"/Applications/QuickTime Player.app"
"/Applications/Reminders.app"
"/Applications/Safari.app"
"/Applications/Self Service.app"
"/Applications/Siri.app"
"/Applications/Stickies.app"
"/Applications/System Preferences.app"
"/Applications/TextEdit.app"
"/Applications/Time Machine.app"
"/Applications/GarageBand.app"
"/Applications/Pages.app"
"/Applications/Numbers.app"
"/Applications/Keynote.app"
"/Applications/iTunes.app"
"/Applications/iBooks.app"
"/Applications/QuestarStudent.app"
"/Applications/NWEA Secure Testing Browser.app"
"/Applications/LockDown Browser.app"
"/Applications/Google Chrome.app")



for del in "${delete[@]}"
do
   array=("${array[@]/$del}") #Quotes when working with strings
done

# iterate through array using a counter
for ((i=0; i<${#array[@]}; i++)); do

    echo "Removing: ${array[$i]}"

    rm -rf "${array[$i]}"


done

View solution in original post

3 REPLIES 3

agetz
Contributor

I was able to get this working, I was making it too complicated.

#!/bin/bash

# create an array with all the filer/dir inside ~/myDir
array=(/Applications/*)

delete=("/Applications/Utilities"
"/Applications/App Store.app"
"/Applications/Automator.app"
"/Applications/Calculator.app"
"/Applications/Calendar.app"
"/Applications/Chess.app"
"/Applications/Contacts.app"
"/Applications/DVD Player.app"
"/Applications/Dashboard.app"
"/Applications/Dictionary.app"
"/Applications/FaceTime.app"
"/Applications/Font Book.app"
"/Applications/Image Capture.app"
"/Applications/Launchpad.app"
"/Applications/Mail.app"
"/Applications/Maps.app"
"/Applications/Messages.app"
"/Applications/Mission Control.app"
"/Applications/Notes.app"
"/Applications/Photos.app"
"/Applications/Photo Booth.app"
"/Applications/Preview.app"
"/Applications/QuickTime Player.app"
"/Applications/Reminders.app"
"/Applications/Safari.app"
"/Applications/Self Service.app"
"/Applications/Siri.app"
"/Applications/Stickies.app"
"/Applications/System Preferences.app"
"/Applications/TextEdit.app"
"/Applications/Time Machine.app"
"/Applications/GarageBand.app"
"/Applications/Pages.app"
"/Applications/Numbers.app"
"/Applications/Keynote.app"
"/Applications/iTunes.app"
"/Applications/iBooks.app"
"/Applications/QuestarStudent.app"
"/Applications/NWEA Secure Testing Browser.app"
"/Applications/LockDown Browser.app"
"/Applications/Google Chrome.app")



for del in ${delete[@]}
do
   array=("${array[@]/$del}") #Quotes when working with strings
done


# iterate through array using a counter
for ((i=0; i<${#array[@]}; i++)); do

    echo "Removing: ${array[$i]}"

    rm -rf "${array[$i]}"


done

agetz
Contributor

Revised again after having issues with files with space. Quotes needed to be around the "${delete[@]}"

#!/bin/bash

# create an array with all the filer/dir inside ~/myDir
array=(/Applications/*)

delete=("/Applications/Utilities"
"/Applications/App Store.app"
"/Applications/Automator.app"
"/Applications/Calculator.app"
"/Applications/Calendar.app"
"/Applications/Chess.app"
"/Applications/Contacts.app"
"/Applications/DVD Player.app"
"/Applications/Dashboard.app"
"/Applications/Dictionary.app"
"/Applications/FaceTime.app"
"/Applications/Font Book.app"
"/Applications/Image Capture.app"
"/Applications/Launchpad.app"
"/Applications/Mail.app"
"/Applications/Maps.app"
"/Applications/Messages.app"
"/Applications/Mission Control.app"
"/Applications/Notes.app"
"/Applications/Photos.app"
"/Applications/Photo Booth.app"
"/Applications/Preview.app"
"/Applications/QuickTime Player.app"
"/Applications/Reminders.app"
"/Applications/Safari.app"
"/Applications/Self Service.app"
"/Applications/Siri.app"
"/Applications/Stickies.app"
"/Applications/System Preferences.app"
"/Applications/TextEdit.app"
"/Applications/Time Machine.app"
"/Applications/GarageBand.app"
"/Applications/Pages.app"
"/Applications/Numbers.app"
"/Applications/Keynote.app"
"/Applications/iTunes.app"
"/Applications/iBooks.app"
"/Applications/QuestarStudent.app"
"/Applications/NWEA Secure Testing Browser.app"
"/Applications/LockDown Browser.app"
"/Applications/Google Chrome.app")



for del in "${delete[@]}"
do
   array=("${array[@]/$del}") #Quotes when working with strings
done

# iterate through array using a counter
for ((i=0; i<${#array[@]}; i++)); do

    echo "Removing: ${array[$i]}"

    rm -rf "${array[$i]}"


done

wltorres
New Contributor II

how do you set it up to delete just one application and not all?