Posted on 07-31-2017 05:41 PM
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
Posted on 07-31-2017 06:21 PM
@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?
Posted on 07-31-2017 07:49 PM
@sdagley was not posted :) ill fix it up. Even with fi - still nothing
Posted on 07-31-2017 11:19 PM
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
Posted on 08-01-2017 12:09 AM
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
Posted on 08-01-2017 12:11 AM
But also onedrive does not normally put that folder on the desktop.
It is normally put in the root level of the home folder
Posted on 08-01-2017 01:04 AM
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 ).
Posted on 08-01-2017 06:27 AM
@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.
Posted on 08-01-2017 07:36 AM
@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.
Posted on 08-07-2017 10:39 PM
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.
Posted on 08-08-2017 09:03 AM
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