添加賬戶的shell腳本

[root@yanjin ~]# vim account.sh

#!/bin/bash

#
# Founded account
#
# History:
# 2012/07/06    JinYan   
PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH
accountfile="user.passwd"
 
# 1. Account information input
read -p "( Input title name, ex> std )======> " username_start
read -p "( Input degree, ex> 1 or enter )=> " username_degree
read -p "( Input start number, ex> 520 )========> " nu_start
read -p "( Input amount of users, ex> 100 )=====> " nu_amount
read -p "1)Password standard 2)Random number from the definition ==> " pwm
if [ "$username_start" == "" ]; then
        echo "No input code not execution" ; exit 1
fi
testing1=`echo $nu_amount | grep '[^0-9]' `
testing2=`echo $nu_start  | grep '[^0-9]' `
if [ "$testing1" != "" ] || [ "$testing2" != "" ]; then
        echo "The input number is wrong, the digital content" ; exit 1
fi
if [ "$pwm" != "1" ]; then
        pwm="2"
fi
 
# 2. Start with the password files output account
[ -f "$accountfile" ] && mv $accountfile "$accountfile"`date +%Y%m%d`
nu_end=$(($nu_start+$nu_amount-1))
for (( i=$nu_start; i<=$nu_end; i++ ))
do
        account=$username_start$username_degree$i
        if [ "$pwm" == "1" ]; then
                password="$account"
        else
                password=""
                test_nu=0
                until [ "$test_nu" == "8" ]
                do
                        temp_nu=$(($RANDOM*50/32767+30))
                        until [ "$temp_nu" != "60" ]
                        do
                                temp_nu=$(($RANDOM*50/32767+30))
                        done
                        test_nu=$(($test_nu+1))
                        temp_ch=`printf "\x$temp_nu"`
                        password=$password$temp_ch
                done
        fi
        echo "$account":"$password" | tee -a "$accountfile"
done
 
# 3. Began to establish account number and password
        cat "$accountfile" | cut -d':' -f1 | xargs -n 1 useradd -m
        chpasswd < "$accountfile"
        pwconv
echo "OK!Establish complete."
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章