Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You have enclosed your find command in the USERLIST assignment in single quotes – that will assign the whole command as a string to the $USERLIST variable (foregoing all shell expansion – that’s what single quotes are for). Instead, to get the command output assigned, you need to enclose it in backticks (`) or in a $(<command>) construct, i.e.

USERLIST=`find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356`

or

USERLIST=$(find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356)
share|improve this answer
    
Thanks! Maybe the original source and the backtick and it somewhere got typed or copied as a single quote ('). –  James Pierce Nov 3 '11 at 21:10
1  
Backticks are confusing in several respects. Just use $( ) instead. –  Gordon Davisson Nov 4 '11 at 2:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.