管理员到普通用户 current_admin_to_common_user_zhong
#!/bin/bash
#
#管理员密码
Passwd=$(/usr/bin/osascript -e 'display dialog "请输入管理员密码" with hidden answer default answer "" with icon note buttons {"Continue"} default button "Continue"
set result1 to result
set the text_data to text returned of the result
set the button_data to button returned of the result1
if button_data = "Cancel" then
return ""
end if
return text_data')
if [ -z "$Passwd" ];then
exit 0;
fi
if [ $Passwd = 'bbbbbbbbbb' ]; then
# admins=$(dscl . -read /Groups/admin | grep GroupMembership | sed -e 's@GroupMembership:@@' -e 's@root@@' -e 's@bbbb-admin@@' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') # (/usr/bin/osascript -e "display dialog \"管理员有 $admins !\" buttons {\"OK\"} default button \"OK\" ") #这一行代码似乎有问题
#userName=$(/usr/bin/osascript -e 'display dialog "请输入需要转换成普通成员的管理员名称!一般是JS-工号,可在 /Users/ 目录下面查看" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
#set the date_data to text returned of the result
#return date_data')
oldUserName=${HOME##*/}
userName=${HOME##*/}
id -Gn "$userName" | grep -i " admin "
if [ $? -ne 0 ];then #判断是否 不是管理员
(/usr/bin/osascript -e "display dialog \" $userName 本身就是普通用户,无须修改为普通用户!\" buttons {\"OK\"} default button \"OK\"")
exit 0;
fi
#小写转换成大写
userName=$(echo "$userName" | tr '[:lower:]' '[:upper:]')
sudo dscl . -merge /Groups/admin GroupMembership $userName
sudo dscl . -delete /Groups/admin GroupMembership $userName
#大写转换成小写
userName=$(echo "$userName" | tr '[:upper:]' '[:lower:]')
sudo dscl . -merge /Groups/admin GroupMembership $userName
sudo dscl . -delete /Groups/admin GroupMembership $userName
sudo dscl . -merge /Groups/admin GroupMembership $oldUserName
sudo dscl . -delete /Groups/admin GroupMembership $oldUserName
dscacheutil -flushcache
id -Gn "$oldUserName" | grep -i " admin "
if [ $? -ne 0 ];then #仍然判断是否 不是管理员 如果成功执行
(/usr/bin/osascript -e "display dialog \"成功把 $oldUserName 修改为普通用户,请重启电脑(或退出登录再登录)!\" buttons {\"OK\"} default button \"OK\"")
else
sudo dseditgroup -o edit -d "$oldUserName" -t user admin # dseditgroup命令再次从admin组中删除 -d 是 delete 删除
(/usr/bin/osascript -e "display dialog \"成功把 $oldUserName 修改为普通用户,请重启电脑(或退出登录再登录)!\" buttons {\"OK\"} default button \"OK\"")
fi
#admins=$(dscl . -read /Groups/admin | grep GroupMembership | sed -e 's@GroupMembership:@@' -e 's@root@@' -e 's@bbbb-admin@@' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') #array=($admins)
#for one_user in "${array[@]}"; do
# sudo dscl . -delete /Groups/admin GroupMembership $one_user
#done
#(/usr/bin/osascript -e "display dialog \" $userName 已转成了普通用户,请重启电脑!\" buttons {\"OK\"} default button \"OK\"")
echo "OK";
fi
普通用户到管理员 current_common_user_to_admin_zhong
#!/bin/bash
#
#管理员密码
Passwd=$(/usr/bin/osascript -e 'display dialog "请输入管理员密码" with hidden answer default answer "" with icon note buttons {"Continue"} default button "Continue"
set result1 to result
set the text_data to text returned of the result
set the button_data to button returned of the result1
if button_data = "Cancel" then
return ""
end if
return text_data')
if [ -z "$Passwd" ];then
(/usr/bin/osascript -e 'display dialog "密码不能为空!" buttons {"OK"} default button "OK"')
exit 0;
fi
if [ $Passwd = 'bbbbbbbb' ]; then
#userName=$(/usr/bin/osascript -e 'display dialog "请输入用户名,一般是JS-工号,可在 /Users/ 目录下面查看" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
#set the date_data to text returned of the result
#return date_data')
oldUserName=${HOME##*/}
userName=${HOME##*/}
id -Gn "$userName" | grep -i " admin "
if [ $? -eq 0 ];then #判断是否 是管理员
(/usr/bin/osascript -e "display dialog \" $userName 本身就是管理员,无须修改为管理员!\" buttons {\"OK\"} default button \"OK\"")
exit 0;
fi
#小写转换成大写
userName=$(echo "$userName" | tr '[:lower:]' '[:upper:]')
sudo dscl . -merge /Groups/admin GroupMembership $userName
#大写转换成小写
userName=$(echo "$userName" | tr '[:upper:]' '[:lower:]')
sudo dscl . -merge /Groups/admin GroupMembership $userName
sudo dscl . -merge /Groups/admin GroupMembership $oldUserName
dscacheutil -flushcache #清空缓存
id -Gn "$oldUserName" | grep -i " admin "
if [ $? -eq 0 ];then #判断是否 是管理员
(/usr/bin/osascript -e "display dialog \"成功把 $oldUserName 修改为管理员,请重启电脑(或退出登录再登录)!\" buttons {\"OK\"} default button \"OK\"")
else
sudo dseditgroup -o edit -m "$oldUserName" -t user admin # dseditgroup 再次从admin组中merge -m 是 merge 合并
(/usr/bin/osascript -e "display dialog \"成功把 $oldUserName 修改为管理员,请重启电脑(或退出登录再登录)!\" buttons {\"OK\"} default button \"OK\"")
fi
echo "OK";
else
(/usr/bin/osascript -e 'display dialog "密码不对!" buttons {"OK"} default button "OK"')
fi