帐户清理注销挂钩在Mavericks中无法正常工作

时间:2014-07-09 17:32:35

标签: macos bash osx-mavericks

我创建了一个bash脚本,可以在注销时运行并删除除特定用户子集之外的用户。代码如下:

#!/bin/bash 
# Logout script to remove nonadmin accounts from Users folder
# Script deletes by name, not by admin rights, as to not mess with 
# group settings.


# TO SET UP SCRIPT, RUN COMMAND sudo defaults write com.apple.loginwindow LogoutHook /some/path/name/to/script


# If user is an admin, exit script 
if [ $USER = "admin" ] || [ $USER = "cas_admin" ] ; then 
echo "LOGOUT: admin folders will not be deleted."

# This for loop is a redundancy system, to delete any user accounts that may have   
# been left over from a quick shut down.

for dir in /Users/*
    do
        if [ $dir != "/Users/admin" ] && [ $dir != "/Users/cas_admin" ] && [ $dir != "/Users/Shared" ]; then 
            echo ${dir}
            rm -R $dir 
            dscl . -delete $dir 
        fi 
    done

exit 1
fi 

# If home directory exists, delete 
if [ -d "/Users/$USER" ]; then 
    echo "LOGOUT: user account cleanup."
    rm -R /Users/"$USER" 
    dscl . -delete /Users/"$USER"   
fi 

# This for loop is a redundancy system, to delete any user accounts that may have   
# been left over from a quick shut down.

for dir in /Users/*
do
    if [ $dir != "/Users/admin" ] && [ $dir != "/Users/cas_admin" ] && [ $dir != "/Users/Shared" ] ; then 
        echo ${dir}
        rm -R $dir 
        dscl . -delete $dir 
    fi 
done

exit 0

此代码在使用LogoutHook注销时执行,在X.6-X.8中完美运行。但是在X.9 Mavericks中,如果直接在bash中运行(删除用户的目录并从用户列表中删除用户),脚本只能正常工作,但是当从LogoutHook运行时,整个/ Users目录获取删除但用户自己没有。如果重新启动计算机并从登录屏幕中选择用户,则计算机将完成Apple初始设置并为相关用户生成新的/ Users目录和新目录。从一点点环顾四周,我已经完成了登录/登出挂钩与小牛队合作的问题,但我见过的其他解决方案似乎都与此问题无关。这将用于计算机实验室设置,我们无法降级到X.8,任何建议?

1 个答案:

答案 0 :(得分:0)

将大部分代码删除后,脚本最终可以运行:

for dir in /Users/*
  do
    if [ ! "$dir" = "/Users/admin" ] && [ ! "$dir" = "/Users/cas_admin" ] && [ ! "$dir" = "/Users/Shared" ] ; then 
      echo ${dir}
      rm -R $dir 
      dscl . -delete $dir 
    fi 
  done

exit 0

遵循@konsolebox建议并将所有权更改为root,rwx-x-x的权限,以及使用不同的文本编辑器(dos to unix headaches)。谢谢你的帮助。