I'm trying to delete old network user accounts off our lab server (Mac OS X 10.6). I found a simple command line command to find user directories that have not been accessed for over a year:
find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356
Running this command successfully lists all of the old accounts. I then found what looks like a simple for loop, but it doesn't work:
#!/bin/bash
keep1="/Volumes/Server-HD2/NetworkUsers/Students/studenttest"
keep2="/Volumes/Server-HD2/NetworkUsers/Students/studenttest2"
USERLIST='find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356'
for a in $USERLIST ; do
[[ "$a" == "$keep1" ]] && continue #skip account 1
[[ "$a" == "$keep2" ]] && continue #skip account 2
echo "Deleting account and home directory for" $a
dscl . delete $a #delete the account
rm -r $a #delete the home directory
done
When I run this command as an executable shell file or just straight in the command line, it loops through $a as being each word in the $USERLIST. In other works if I simply do this:
USERLIST='find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356'
for a in $USERLIST ; do
echo $a
done
It will return:
find
/Volumes/Server-HD2/NetworkUsers/Students
-type
d
-maxdepth
1
-mindepth
1
...
whereas I would expect it to return each directory that meets the "find" command.
I need to figure out how to make an array of the results from the find command to loop through. I guess I simply don't understand how the arrays and strings are handled in bash. Any thoughts?