#!/bin/bash#filename:newuser.sh#Display a menuecho -------------
echo"1 adduser"echo"2 deluser"echo"3 createdir"echo"4 deldir"echo"5 exishell"#Read and excute the user's selectionecho -n "Enter Choice:"read CHOICE
case"$CHOICE"in1)#add user echo"Please enter the number of users you want to add"read NUM
echo"Whether to create a user home directory? 1/0"read CHOME
if [ $CHOME-eq1 ]
then
udir="-m"else
udir="-M"fiecho"Whether to set expiration time? 1/0"read CDATE
if [ $CDATE-eq1 ]
thenecho"Please enter the time MM/DD/YY"read ETIME
ifecho$ETIME |grep -Eq "[0-9]{2}/[0-9]{2}/[0-9]{2}" && date -d$ETIME +%x >/dev/null 2>&1then
utime="-e $ETIME"elseecho"Wrong date format"exit1fielse
utime=''fiecho"Whether user groups are set 1/0?"read CGROUP
if [ $CGROUP-eq1 ]
thenecho"Please enter the user group name"read GNAME
#create group if not exists
egrep "^$GNAME" /etc/group >& /dev/null
if [ $? -ne0 ]
thenecho"User groups that do not exist will be created automatically"
groupadd $GNAME
ugroup="-g $GNAME"else
ugroup="-g $GNAME"fielse
ugroup=''fi#for i in `seq 1 $NUM`for ((i=1;i<=$NUM;i++))
do
id -u user$i >/dev/null 2>&1if [ $? -ne0 ]
then
pw=`cat /dev/urandom | head -n 10 | md5sum | head -c 6` #Gets a 6-digit random number
useradd $udir$utime$ugroup user$iecho"user$i$pw" >> /root/pw.txt #Write the account password to the fileecho"$pw" | passwd --stdin user$ielselet NUM=NUM+1fidoneecho"add user successful";;
2)#del userecho"Please enter the number of users to delete"read DELNUM
uid=`awk -F ':''{print $3}' /etc/passwd`
user=`awk -F ':''{print $1}' /etc/passwd`
array=($user)
j=0
n=0for i in$uid;doif [ $i-gt1000 ];thenif [ $n-lt$DELNUM ];thenif [ ! -d"/home/${array[$j]}" ]
then
userdel "${array[$j]}"#user dir not createelse
userdel -r "${array[$j]}"fi
sed -i '1d' /root/pw.txt #del file user infolet n=n+1elsebreakfifilet j=j+1doneecho"del user success!!!";;
3)
echo"Please input you want create dir nums"read DIRNUM
echo"Please set dir chmod (ug+rwx) u user g group"read STR
for i in `seq 1$DIRNUM`
do
mkdir /root/dir$i
chmod $STR /root/dir$idoneecho"create successfull";;
4)
echo"Please input you want delete dir nums"read DIRNUM
for i in `seq 1$DIRNUM`
do
rm -rf /root/dir$idoneecho"delete successfull";;
5) exit1;;
*) echo"Sorry $CHOICE is not a valid choice"exit1;;
esac