写一个shell脚本,脚本类型为bin/ bash,创建一个用户组group2024,在该组下创建五个用户删除创建的五个用户,删除用户组group2024
时间: 2024-09-12 14:15:13 浏览: 37
当编写一个bash shell脚本来完成上述任务时,你需要遵循一定的步骤。这里是一个基本示例:
```bash
#!/bin/bash
# 创建用户组 group2024
groupadd -g 2024 group2024
# 检查用户组是否存在
if [ $? -eq 0 ]; then
echo "User group created successfully."
else
echo "Failed to create user group."
fi
# 遍历并创建5个用户,每个属于 group2024
for i in {1..5}; do
useradd -u $(($i + 5000)) -g group2024 user$i
done
# 检查用户是否成功创建
echo "Checking if users were added..."
for user in user{1..5}; do
id $user &>/dev/null && echo "$user exists"
done
# 删除所有用户
for user in user{1..5}; do
userdel $user || echo "Failed to delete $user"
done
# 检查用户是否已删除
echo "Checking if users were deleted..."
for user in user{1..5}; do
id $user &>/dev/null && echo "$user still exists" || echo "$user removed"
done
# 删除用户组
groupdel group2024
# 检查用户组是否已删除
if [ $? -eq 0 ]; then
echo "Group deleted successfully."
else
echo "Failed to delete group."
fi
# 结束脚本
exit 0
```
这个脚本首先创建用户组,然后循环创建5个用户,并将它们加入到刚刚创建的用户组。接着检查用户是否创建成功,并删除这些用户。最后删除用户组并确认其已被删除。
阅读全文