Skip to main content

Hi all! I am working on something pretty simple, but I have gotten myself stuck and promise to give myself 5 lashes with a wet noodle for my oversight....



What I am trying to do is display a message to the user to clear the web cache or cancel the operation, if the user chooses to continue my script should run then display the completed message. If the user selects cancel the script should exit. I feel like I am missing something pretty basic. Thoughts?



#!/bin/sh

# the purpose of this script is to delete all browser caches.
#
#
# created by Pat B 11/3/14

CLEARCACHE='/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Internet Browser Web Cache" -heading "Please exit all web pages before continuing" -alignHeading center -description "You web browser cache will be cleared when you click the button below. This may take several minutes and you will be notified when the process completes. Thank you for your patience!" -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns -button1 "Erase Cache" -button2 "Cancel" -defaultButton 1 -cancelButton 2'
USER=`who | grep console | awk '{print $1}'`

# If the user clicks Erase Cache

if [ "$CLEARCACHE" == "0" ]; then
echo "Clearing Cache"
sleep 2
rm -R /Users/$USER/Library/Caches/Google
rm -R /Users/$USER/Library/Caches/Firefox
rm -R /Users/$USER/Library/Caches/com.apple.Safari
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Internet Browser Web Cache" -heading "Process Complete" -alignHeading center -description "Your web browser caches have been cleared for Firefox, Safari, and Google Chrome." -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarSitesFolderIcon.icns -button1 " Exit " -defaultButton 1
exit 1

# if the user clicks cancel
elif [ "$CLEARCACHE" == "2" ]; then
echo "User canceled cache clear";
exit 1
fi

Might be a little different but I would do it this way:



USER=`who | grep console | awk '{print $1}'`

/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Internet Browser Web Cache" -heading "Please exit all web pages before continuing" -alignHeading center -description "You web browser cache will be cleared when you click the button below. This may take several minutes and you will be notified when the process completes. Thank you for your patience!" -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns -button1 "Erase Cache" -button2 "Cancel" -defaultButton 1 -cancelButton 2

# If the user clicks Erase Cache

if [ "$?" == "0" ]; then
echo "Clearing Cache"
sleep 2
rm -R /Users/$USER/Library/Caches/Google
rm -R /Users/$USER/Library/Caches/Firefox
rm -R /Users/$USER/Library/Caches/com.apple.Safari
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Internet Browser Web Cache" -heading "Process Complete" -alignHeading center -description "Your web browser caches have been cleared for Firefox, Safari, and Google Chrome." -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarSitesFolderIcon.icns -button1 " Exit " -defaultButton 1
exit 1

# if the user clicks cancel
elif [ "$?" == "2" ]; then
echo "User canceled cache clear";
exit 1
fi

When you are defining CLEARCACHE, you have it wrapped in single quotes, instead of back ticks or $().


@davidacland, you nailed it, thanks! Can you link me to a page that describes the usage of "$?" thanks a ton for your help.



Pat


@pat.best I made a few changes to how things were quoted and it worked for me. Thanks for posting yours as well @davidacland



#!/bin/sh

# the purpose of this script is to delete all browser caches.
#
#
# created by Pat B 11/3/14

CLEARCACHE=$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Internet Browser Web Cache" -heading "Please exit all web pages before continuing" -alignHeading center -description "You web browser cache will be cleared when you click the button below. This may take several minutes and you will be notified when the process completes. Thank you for your patience" -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns -button1 "Erase Cache" -button2 "Cancel" -defaultButton "1" -cancelButton "2")
USER=$(who | grep console | awk '{print $1}')

# If the user clicks Erase Cache

if [ "$CLEARCACHE" == "0" ]; then
echo "Clearing Cache"
sleep 2
rm -R "/Users/$USER/Library/Caches/Google"
rm -R "/Users/$USER/Library/Caches/Firefox"
rm -R "/Users/$USER/Library/Caches/com.apple.Safari"
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Internet Browser Web Cache" -heading "Process Complete" -alignHeading center -description "Your web browser caches have been cleared for Firefox, Safari, and Google Chrome." -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarSitesFolderIcon.icns -button1 " Exit " -defaultButton "1"
exit 1

# if the user clicks cancel
elif [ "$CLEARCACHE" == "2" ]; then
echo "User canceled cache clear";
exit 1
fi

No worries! $? is basically the exit code of the last command. I normally use it in scripts to check that the last command exited as 0 (0 = good) before proceeding.



This page has some extra info: http://www.tldp.org/LDP/abs/html/exit-status.html


thanks to the both of you!