编辑代码

#!/bin/bash  
  
# 检查参数数量是否正确  
if [ $# -ne 1 ]; then  
    echo "Usage: sudo bash createclassuser.sh <student_list_file>"  
    exit 1  
fi  
  
# 获取参数  
student_list_file=$1  
  
# 检查学生名单文件是否存在  
if [ ! -f "$student_list_file" ]; then  
    echo "Error: Student list file not found!"  
    exit 1  
fi  
  
# 遍历学生名单文件中的每一行  
while IFS='' read -r line; do  
    # 分割每一行数据,获取学号、姓名全拼和姓名中文  
    id=$(echo $line | awk -F',' '{print $1}')  
    name_en=$(echo $line | awk -F',' '{print $2}')  
    name_ch=$(echo $line | awk -F',' '{print $3}')  
  
    # 创建用户,设置密码为用户学号,用户全称为学号+姓名中文,用户组为班级名(参数1给出)  
    username="$name_en$id"  
    password="$id"  
    full_name="$id$name_ch"  
    useradd -m -p "$password" -g "$class_name" -s /bin/bash -u "$id" -c "$full_name" "$username"  
done < "$student_list_file"