Skip to main content
Question

Script Help, EA for Folder

  • August 1, 2017
  • 10 replies
  • 20 views

Forum|alt.badge.img+4

Hey guys, newbie in this and myself and JAMF been trying to get the following to work but we are not getting much luck. Any thoughts?

#!/bin/bash
USER=$(ls -l /dev/console | awk '{print $3}')
DIRECTORY="/Users/$USER/Desktop/OneDrive - Bunbury Catheral Grammar School"
if [ ! -d "$DIRECTORY" ]; then

echo "<result>Not Present</result>"

else

echo "<result>Directory Exists</result>"

fi

It seems to not finding the directory itself as no matter what, the outcome is Directory Exist even when its not.

The OneDrive folder is located in the User Directory. Macintosh HD/User/username/Desktop

10 replies

sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • August 1, 2017

@rsgrammar In the snippet you posted there's no fi to terminate your if statement. Did that not get posted, or is it actually missing in your EA script?


Forum|alt.badge.img+4
  • Author
  • Contributor
  • August 1, 2017

@sdagley was not posted :) ill fix it up. Even with fi - still nothing


Forum|alt.badge.img+15
  • New Contributor
  • August 1, 2017

The spaces after OneDrive are the problem I think, the correct syntax is to have a " " in the path
Drag the location into terminal and it will add the correct syntax for you. it will be something like this:
OneDrive - Bunbury Catheral Grammar School


rickgmac
Forum|alt.badge.img+9
  • Valued Contributor
  • August 1, 2017

How about using something like this

#!/bin/bash

USER=$(ls -l /dev/console | awk '{print $3}')
OneDrive=`ls -al /Users/"$USER"/Desktop | grep "OneDrive - Bunbury Catheral Grammar School"`
if [ ! "$OneDrive" = "" ]; then
echo "<result>Present</result>"
else
echo "<result>Not Present</result>"
fi

rickgmac
Forum|alt.badge.img+9
  • Valued Contributor
  • August 1, 2017

But also onedrive does not normally put that folder on the desktop.

It is normally put in the root level of the home folder


Forum|alt.badge.img+3
  • New Contributor
  • August 1, 2017

Your Script works.
If it says Directory exists even if it doesn't exist, then problem could be that you either don't have permission on that Desktop folder or The One Drive Folder's name is different.(Sometimes the small and long "-" also makes the difference ).


sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • August 1, 2017

@rsgrammar Take another look at the name of the folder you're using in your script if what you posted is a copy and paste of your EA. I think you misspelled Cathedral.


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • August 1, 2017

@Rudolph The spaces and characters in the path are not the issue as the entire path is surrounded by double quotes, so nothing should need to be escaped.

I think @sdagley may be on to something though. The "Catheral" in the folder name threw me as well since I was expecting it to say "Cathedral" Seems like it might be missing the "d" and as such is not finding the actual folder, which may have the extra character in it. I would look at that, because I don't really see any reason the script shouldn't work.

I would just make two minor suggestions, which is to use stat for finding the current logged in user over ls as I find it's more compact and precise. Also, I would use a different variable name than $USER as bash uses $USER as a built in environment variable. I don't think it's interfering, but it would be better to use something else, just in case.


Forum|alt.badge.img+4
  • Author
  • Contributor
  • August 8, 2017

Hi all, thanks for all your help, we finally got it to work :)

#!/bin/bash

USER=$(ls -l /dev/console | awk '{print $3}')
OneDrive=`ls -al /Users/"$USER"/Desktop | grep "OneDrive - Bunbury Cathedral Grammar School"`
if [ ! "$OneDrive" = "" ]; then
echo "<result>Present</result>"
else
echo "<result>Not Present</result>"
fi

Sorry for the typos and beginners error :) still new to scripting but learning.


Forum|alt.badge.img+7
  • Contributor
  • August 8, 2017

not sure why you are testing the existence of a directory with ls | grep. Your initial script was using the appropriate tools, I have to assume there was a typo or something.

#!/bin/bash

loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
loggedInUserHome=$(dscl . read /Users/"$loggedInUser" NFSHomeDirectory | awk '{print $NF}')

dir="${loggedInUserHome}/Desktop/OneDrive - Bunbury Cathedral Grammar School"

if [[ -d $dir ]]; then
    echo "<result>Present</result>"
else
    echo "<result>Not Present</result>"
fi