Posted on 09-24-2021 09:48 AM
I need to look for four folders that should have been automatically added to each user's Documents folder. I wrote a simple script that should check if the folders exist. If they don't, it will run the Jamf policy that creates them. The problem I keep running into is that when I test the script, the results tell me that the folders exist even when they don't. Obviously I'm doing something wrong but I can't figure out what. Please help! The script is below. If I manually type a command the commands in Terminal, I can get an accurate response. The script tells me that the folders are present whether they are or not. What the heck am I doing wrong?
#!/bin/sh
###Looks for PDF folders in a user's Documents folder. If the folders are not present this script will run the Jamf policy that creates them.
currentuser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
echo $currentuser
pdfFolders=$(
/Users/$currentuser/Documents/"PDFs for Client Reports"
/Users/$currentuser/Documents/"PDFs for ATBs"
/Users/$currentuser/Documents/"PDFs for Estimates"
/Users/$currentuser/Documents/"PDFs for Insertion Orders"
)
if [ -d $pdfFolders ]
then echo "Folders present"
else echo "Folders not present"; jamf policy -event install-fmfolders
fi
Solved! Go to Solution.
09-24-2021 11:33 AM - edited 09-24-2021 06:24 PM
I think you might need to iterate over the values in the array of directories. Something like:
for i in "${pdfFolders[@]}"
do
if [ -d $i ]
then
echo "Folder $i present"
else
echo "Not all folders are present...running policy to add"
jamf policy -event install-fmfolders
break
fi
done
Pretty sure the "$" needs to be left out when declaring the array as well:
pdfFolders=(...)
09-24-2021 11:33 AM - edited 09-24-2021 06:24 PM
I think you might need to iterate over the values in the array of directories. Something like:
for i in "${pdfFolders[@]}"
do
if [ -d $i ]
then
echo "Folder $i present"
else
echo "Not all folders are present...running policy to add"
jamf policy -event install-fmfolders
break
fi
done
Pretty sure the "$" needs to be left out when declaring the array as well:
pdfFolders=(...)
Posted on 09-24-2021 11:46 AM
Interesting. I'm confused now since I have other scripts that use $ after =. They work but this one won't. I will give this a try.
Posted on 09-24-2021 11:51 AM
This works! THANK YOU!!!
Of course now I have the mystery of why other scripts work that use $ after = but I will deal with that later 😊
Posted on 09-24-2021 12:37 PM
@howie_isaacks You're probably getting confused with the difference between an array and a variable. A variable can either just be a string, such as:
somename="hello"
So when somename is called later it equals hello.
Or you can set a variable with the output of a command, such as:
somename=$(somecommand)
And somename then becomes the results of somecommand
With an array it just needs parens to enclose the contents. The $ is not valid for declaring a shell array.
somearray=(one two three)
With the above, when iterating over it, it will print out
one
two
three
Hope that helps clarify it a bit.
Posted on 09-24-2021 12:40 PM
Thanks! I remember that.... now 😎 I appreciate all the help!