Check My Work, script isn't working

BuddyBrett
New Contributor

Hey All,

I'm working on a script that deletes the Adobe Media Cache. If you work with Adobe at enterprise level, then you know what a mess this thing causes. Basically all I am trying to is delete two folders in the users library location. I have the script working when run locally, but will not work when ran from remote, or via a policy. As a side note, I am a workstation tech with a producing background, and fairly new to scripting. Please be gentle :) Any help would be greatly appreciated. Contents of the script below.

!/bin/sh

Cache Buster.sh

Created by Brett Ramker on 7/6/15.

RM -r /Users/$USER/Library/Application Support/Adobe/Common/Media Cache/
RM -r /Users/$USER/Library/Application Support/Adobe/Common/Media Cache Files/**

7 REPLIES 7

emily
Valued Contributor III
Valued Contributor III

Any chance you can re-post using the forum formatting to get a better idea of the formatting?

How are you defining $USER?

BuddyBrett
New Contributor

Yea sure!

My understanding was that if you use $USER, OS X will plug in the current logged in user.

emily
Valued Contributor III
Valued Contributor III

So when you run it as you, locally, it knows that you are $USER. But I believe if you're trying to automate this you'll need to define $USER.

Something like:

USER=`ls -l /dev/console | cut -d " " -f 4`

BuddyBrett
New Contributor

That worked, thanks so much!

emily
Valued Contributor III
Valued Contributor III

Sure thing!

Don't forget to mark a post as an answer so people know you got the help you needed. :)

mm2270
Legendary Contributor III

EDIT: I'll leave my post below just in case, but it looks like you're all set!

As @emilykausalik hinted at, the $USER variable may not be getting defined correctly. A general rule to follow when scripting for Casper deployed scripts is to define any user names in your script by getting the logged in user, or cycling over user directories, etc. I would not rely on built in bash variable stuff because the scripts are being run under a root context by default, so it changes the whole interaction. Its not the same as running a script from the Terminal directly.

You can get the logged in user (assuming someone is usually logged in) by doing something like

USER=$(ls -l /dev/console | awk '{print $3}')

There are about 4-5 other ways to do this though, including using stat, python code and a few others. Use whatever works, but get the username within the script and then plug that variable into the rm lines and see what happens.
Also make sure 'rm' is lower case. I can't tell if the upper case of those in your post was just because of auto correct or if that's actually how they were written. 'RM' will not work at all.

nessts
Valued Contributor II

might want to spell RM as rm, although on a case insensitive OS I suppose that might work. assuming all the # are missing because they made the text bold in the post, when posting scripts use the >_ button it gives you a pretty good hint that its the right thing by giving you a

#!/bin/sh

to know that its a script section...
after you get your $USER variable sorted your may need to consider why you have two asterisks on the second rm command I probably would just remove both of them.