简介:大学时,老师在一台linux服务器上为我们班每个人创建了一个普通用户,每个用户文件夹路径为/home/班级名/用户名+学号,然后要求做一个显示未登录学生名单的shell脚本,方便考勤,以下是一些思考。
我们知道Linux用户名相关信息保存在文件夹/etc/passwd(密码加密),组名相关信息保存在文件夹/etc/group,而密码则保存在/etc/shadow(需管理员权限查看)。接着上完整shell脚本代码。
#!/bin/bash
File1="login_student.txt"
File2="all_student.txt"
#将/etc/passwd里的总学生名单保存到"all_student.txt"。格式为:学号:姓名
`cat /etc/passwd|cut -d ":" -f 1,5|tail -n 53 > $File2`
#将登陆人数重定向到"login_student.txt"。格式为:学号,(ip地址)
`who|tr -s " "|tr " " ","|