bash脚本为登录时的用户创建文件夹并设置访问权限

ori*_*ush 1 macos bash logout

我是一所小型艺术学校的新摄影技师,负责一个小型的实验室.不幸的是我们的IT部门.不要直接支持mac(只有网络),所以我必须卷起袖子,随便学习.

该实验室已在所有机器上作为单个管理登录(无密码)运行多年,这导致了很多困难,但我正在努力将其更改为接受Active Directory凭据的模块,从而创建新用户在飞行中.注销时,将销毁该用户帐户以在每个会话之间规范化用户环境.

缺点是,对于在特定站点重复工作的少数学生,他们创建的任何文件当然都会被销毁.作为一种解决方法,我想创建一个登录脚本,为他们创建一个安全的位置,以便将他们的文件保存在分区的驱动器上,并将该目录的权限设置为仅为除admin之外的所有其他用户编写.

我已经将大部分内容拼凑在了一起 - 但我真的很喜欢我的更好的一般反馈(这是我在这里做的任何事情都是不明智的),关于如何设置权限的指导就像我在这里所做的那样似乎不起作用,坦率地说,我对UNIX权限的来龙去脉不了解.

我已将其设置为具有执行权限的.command文件以对其进行测试 - 并且按预期创建文件夹和别名,但所有者/组设置未执行. - 所有者是我的管理员帐户,该组仍然是"员工".我还建立了一个"测试"用户和一个学生组.

非常感谢你的想法.

#!/bin/bash
# Create a safe place for users to save their files
# We will destroy this directory if it's empty on log out
# Using sudo for the time being for testing, wont have to when it's run at login by root

# Dont run if user is admin
if [ `whoami` == "comdesadmin" ]; then
exit 1
fi

if [ `whoami` != "comdesadmin" ]; then

#User name
USERNAME="test";
#USERNAME=`whoami` ;

# Group name
USERGROUP=student;
#Set direcory path
USERSAFEFOLDER=/Volumes/nest/$USERNAME

# Create the directory for the user if it isn't there
# user names shouldn't have any spaces so this should be ok
mkdir -p $USERSAFEFOLDER;

# Set the ownership to the current users (as this be being run by root)
sudo chown -R guest $USERSAFEFOLDER;

# Set the group
sudo chgrp $USERGROUP $USERSAFEFOLDER;

# Set write only to everyone else
chmod 700 $USERSAFEFOLDER;

# Make alias on the desktop for them
osascript <<END_SCRIPT
tell application "Finder"
   make new alias to folder (posix file "$USERSAFEFOLDER") at desktop
end tell
END_SCRIPT

echo "Sucess";

fi
exit 0
Run Code Online (Sandbox Code Playgroud)

Sto*_*ica 5

  1. 第二个if是完全不必要的(if [ `whoami` != "comdesadmin" ]),因为它与第一个完全相反.您可以简单地删除该行(及其fi在底部的结束)

  2. 将第一行更改#!/bin/bash -e为脚本在步骤失败时立即退出的方式可能是个好主意

  3. 而不是包括sudo进行测试,没有sudo行测试更容易和更好,并且通过调用脚本本身sudo作为要测试的用户,例如:

    sudo -u username -g groupname .command
    
    Run Code Online (Sandbox Code Playgroud)
  4. 运行时不应该使用chownchgrpsudo

  5. 所述exit 0由于在端部没有必要echo之前正确的命令将与0退出其将被用作脚本本身的退出代码

  6. ;在在线的端部是不必要

  7. 最好$()是以各种方式使用反引号,例如$(whoami)代替`whoami`