写一个shell脚本,脚本类型为bin/ bash,创建一个用户组geoup2024,在该组下创建五个用户删除创建的五个用户,删除用户组group2024
时间: 2024-09-12 10:15:02 浏览: 29
当编写一个`.bin/bash`类型的shell脚本来完成这个任务时,你需要考虑以下几个步骤:
```bash
#!/bin/bash
# 创建用户组 group2024
groupadd -g 2024 geoup2024
if [ $? -ne 0 ]; then
echo "Failed to create the group: geoup2024"
exit 1
fi
echo "Group created: geoup2024"
# 创建五个用户,并将他们加入到 group2024
for ((i=1; i<=5; i++)); do
useradd -u $(($i+1000)) -g geoup2024 user$i
if [ $? -ne 0 ]; then
echo "Failed to create user $i"
break
fi
done
echo "Users created: user1 to user5"
# 检查所有用户是否已成功添加到 group2024
for user in user1 user2 user3 user4 user5; do
groups $user | grep -q geoup2024
if [ $? -eq 0 ]; then
echo "$user is part of group geoup2024"
else
echo "Error: $user not added to geoup2024"
fi
done
# 删除这五个用户
for user in user1 user2 user3 user4 user5; do
userdel -r $user
if [ $? -ne 0 ]; then
echo "Failed to delete user $user"
else
echo "User $user deleted"
fi
done
# 删除用户组 group2024
groupdel geoup2024
if [ $? -ne 0 ]; then
echo "Failed to delete the group: geoup2024"
else
echo "Group geoup2024 deleted"
fi
exit 0
```
阅读全文