I need help with a script using "if" an "then"

howie_isaacks
Valued Contributor II

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

 

1 ACCEPTED SOLUTION

dwbergstrom
New Contributor III

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=(...)

 

 

 

 

View solution in original post

5 REPLIES 5

dwbergstrom
New Contributor III

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=(...)

 

 

 

 

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.

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 😊

mm2270
Legendary Contributor III

@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.

 

howie_isaacks
Valued Contributor II

Thanks! I remember that.... now 😎 I appreciate all the help!