首页 > 代码库 > Linux命令:自动添加用戶
Linux命令:自动添加用戶
1.自动添加10用戶:
#adminuser.sh
#!/bin/bash
#
if [ $1 == ‘--add‘ ]; then
forI in {1..10}; do
if id user$I &> /dev/null; then
echo "user$I exists."
else
useradd user$I
echo user$I | passwd --stdin &> /dev/null
echo "add user$I finished."
fi
done
elif [ $1 == ‘--del‘ ]; then
forI in {1..10}; do
if id user$I &> /dev/null; then
userdel -r user$I
echo "Delete user$I finished."
else
echo "No user$I."
fi
done
else
echo"$1 is a Unknown ARG"
exit8
fi
2.自动添加用戶:adminuser2.sh --add USER1,...|--del user1,user2,... |--help
# vi adminuser2.sh
#!/bin/bash
if [ $# -lt 1 ]; then
echo"Usage:adminuser2.sh --add USER1,...| --del user1,user2 | --help"
exit7
fi
if [ $1 == ‘--add‘ ]; then
forI in `echo $2 | sed ‘s/,/ /g‘`; do
ifid $I &> /dev/null; then
echo "$I exists."
else
useradd $I
echo $I | passwd --stdin $I &> /dev/null
echo "add $I finished."
fi
done
elif [ $1 == --del ]; then
forI in `echo $2 | sed ‘s/,/ /g‘`; do
ifid $I &> /dev/null; then
userdel -r $I
echo "Delete $I finished."
else
echo "$I not exist."
fi
done
elif [ $1 == --help ]; then
echo"Usage:adminuser2.sh --add USER1,...| --del user1,user2 | --help"
else
echo"Unkown options."
fi
---end---
Linux命令:自动添加用戶