通过shell脚本添加新用户

一次添加一个用户

#!/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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值