一次添加一个用户
#!/bin/bash
#create date:2010/01/25
#modify date:2010/01/30
#add a new tiptop os user
first_input_username=true
first_input_password=true
#read the password
while [ -z $username ]; do
if [ "$first_input_username" == "true" ]; then
first_input_username=false
echo -n "Please input the username:"
else
echo "The username must be not empty"
echo -n "Please input the username:"
fi
read username
done
#check if the user is exist
#add date:2010/01/30
user=`awk -F : '{print $1}' /etc/passwd | grep "\"`
if [ "$username" == "$user" ]; then
echo "The user exist"
exit 0
fi
#read the password
while [ -z $password ]; do
if [ "$first_input_password" == "true" ]; then
first_input_password=false
echo -n "Please input the password:"
else
echo "The password cann't be empty"
echo -n "Please input the password:"
fi
read -s password
echo ""
done
#add user,the default user's shell is ksh
useradd -g tiptop $username -s /bin/ksh
#set the password
echo "$username:$password"|chpasswd
cd /home/$username
if [ -f .profile ]; then
mv .profile .profile_`date +%Y%m%d%H%m`.bak
fi
ln -s /u/tiptop/profile/it.profile .profile
echo "Congratulation,add new user successful"
echo "The username is:$username"
echo "The password is:$password"
echo "Please remober the information above"
一次添加多个用户:
#!/bin/bash
#create date:2010/01/30
#batch add tiptop os user
logfile_path=`pwd`
logfile_name=add_users_`date +%Y_%m_%d`_00.log
i=1
while [ -f $logfile_name ]; do
logfile_name=add_users_`date +%Y_%m_%d`_0$i.log
let i+=1
done
cat users.txt | while read line
do
#get the username and password
username=`echo $line | awk '{print $1}'`
password=`echo $line | awk '{print $2}'`
if [ -z $password ]; then
password=oak88
fi
#check if the user exists
user=`awk -F : '{print $1}' /etc/passwd | grep "\"`
if [ "$username" == "$user" ]; then
echo "The user $username exists"
echo "User: username creates failure" >> $logfile_name
continue
fi
#add user,the default user's shell is ksh
useradd -g tiptop $username -s /bin/ksh
#set the password
echo "$username:$password"|chpasswd
cd /home/$username
if [ -f .profile ]; then
mv .profile .profile_`date +%Y%m%d%H%m`.bak
fi
ln -s /u/tiptop/profile/it.profile .profile
echo "User $username created"
done
echo "You can get the failure list from:$logfile_path/$logfile_name"
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7419833/viewspace-626430/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7419833/viewspace-626430/