Help making Bash Script work with Lion?

Discussion in 'Mac Programming' started by Yujenisis, Sep 12, 2011.

  1. Yujenisis, Sep 12, 2011
    Last edited by a moderator: Sep 12, 2011

    Yujenisis macrumors 6502

    May 30, 2002
    At work (where we sell Macs and often install software for customers) we use an old script to quickly delete the test user account we create and set computers back to their default settings so that customers can experience that 'out-of-the-box' experience. :apple:

    The script was original designed for Leopard but worked perfectly with Snow Leopard no longer functions properly in Lion. In fact, after running the script the Lion set up sequence beach balls and cannot be completed without first re-installing the entire OS.

    I am awfully new to the terminal so I don't know where to look. The script is quoted below:

    /sbin/mount -uw /
    echo "Found the following users:"
    ls -1 /Users/ | grep -v "Shared" | grep -v "Deleted Users" | grep -v "\."
    declare -a userarray
    userarray=( `ls /Users/ | grep -v "Shared" | grep -v "Deleted Users" | grep -v "\."` )
    for (( i = 0 ; i < ${#userarray[@]} ; i++ ));
    	   echo -n "Delete user ${userarray[i]} (Y/N)?: "
               read -n 1 answer
               case "$answer" in
    		   rm -Rf "/Users/${userarray[i]}/"
    		   echo ""
                       echo "${userarray[i]} deleted."
    		    echo ""
                        echo "${userarray[i]} NOT deleted."
      		    echo ""
                        echo "$answer unknown. Please answer Y or N." 
    		    let i=(i-1)
       echo ""
    rm -Rf "/Users/Deleted Users/"
    if [ -d /var/db/dslocal ]; then
         rm -Rf /var/db/dslocal
         mkdir -p /var/db/dslocal/nodes
         cp -Rp /System/Library/DirectoryServices/DefaultLocalDB/Default /var/db/dslocal/nodes/
         cp -Rp /System/Library/DirectoryServices/DefaultLocalDB/dsmappings /var/db/dslocal/
         rm -rf /private/var/db/netinfo
         rm -rf /private/var/db/openldap
         rm -rf /private/var/db/samba
         rm -rf /private/var/db/dhcpclient
         rm -rf /var/db/.AppleSetupDone 
         rm -rf /Library/Caches
         rm -rf /Library/Logs
         rm -rf /Library/Preferences
         rm /missingkitty
    echo "MissingKitty successfully removed user databases."
    echo ""
    echo "Press any key to shutdown."
    read -n 1 nothing
    /sbin/fsck -fy
    shutdown -h now
    Any help would be most appreciated! Thank you.
  2. chown33 macrumors 604

    Aug 9, 2009
    1. I take it the script runs when logged in as root? Or booted in single-user mode?

    2. Where does it hang, in the loop deleting users, or afterwards, when it's deleting and copying other files?

    If you don't know where it hangs, then you need to run it in a way that tells you what it's doing. Add this line to the top of your current shell script:
    set -x
    This will cause the shell to echo each simple command before it's executed. Shell variables etc. in the output will be expanded. See the man page for bash, and find PS4 on the page. Read the description of the -x option at one of the few places where PS4 is found.

    To disable the command-echoing:
    set +x

    If I had to guess, I'd say it's hanging after the deletion of users, at some point in the removal or copying of other files.

    If that's so, the obvious conclusion is that one or more of the files being removed or copied is no longer removable. Determining exactly which one is going to need the detailed line-by-line output from set -x.
  3. Yujenisis, Sep 12, 2011
    Last edited: Sep 12, 2011

    Yujenisis thread starter macrumors 6502

    May 30, 2002
    Thanks for your response chown33. I should clarify, that the script runs just fine and appears to succeed as the next time the machine is turned on the Mac runs the initial set up wizard as its supposed to when it detects no users have been created. We run it in single user mode (Command + S on startup).

    The trouble begins when the computer is next turned on and the user follows Apple's own set up wizard: at some point the wizard pinwheels and cannot progress unless the OS is reinstalled.

    I don't suppose you have any additional thoughts with this new information? Thanks.
  4. larkost macrumors 6502a

    Oct 13, 2007
    Why don't you just re-image the computer. Tools like InstaDMG can make you a never-booted image from the Install.esd image, and even do any updates that you provide.

    You are never going to be able to keep up with all the spots that things get put (you are missing /var/folders for instance), so why try?
  5. Cold--, Sep 13, 2011
    Last edited: Sep 16, 2011

    Cold-- macrumors newbie

    Sep 13, 2011
    Try this on a test machine. I wrote a similar script for 10.6. This must work on Lion, too (I can't imagine they got rid of Directory Services). Check it out!

    DSpath = "/System/Library/LaunchDaemons/"
    echo -n "Mounting the drive…\n\n"
    mount -uw / 
    echo -n "Launching OS X Directory Services…\n\n"
    launchctl -load ${DSpath}Local.plist 2>/dev/null
    launchctl -load ${DSpath}.plist 2>/dev/null
    echo -n "Here's a list of users:\n"
    dscl . list /Users | grep -v -e _ -e daemon -e nobody -e root
    echo -n "\n\nWhich user would you like to remove?\n"
    read -a sysUser
    echo -n "\n\nAre you sure you want to delete ${sysUser}? (Enter 'y' or 'n')"
    read -a confirm
    if [ ${confirm} = "y" ]
      echo -n "\n\nRemoving \"${sysUser}\" from Directory Services…\n\n"
      dscl . -delete /Users/${sysUser}
      echo -n "Removing ${sysUser}'s home directory"
      rm -Rf /Users/${sysUser}
      echo -n "Cleaning up the cache directory and removing .AppleSetupDone file."
      rm -Rf /Library/Caches/*
      rm -Rf /private/var/db/.AppleSetupDone
      echo -e "\n\n\n\nAll is well. Cold-- saved your missingkitty from Lion disaster. >:3\n\n\n\nShutting down the machine…"
      shutdown -h now
      echo -n "Did not read a 'y' input. Exiting the script for safety. No user data was harmed."

Share This Page