從鳥哥的範例和visual basic的概念中寫出來的
#!/bin/bash
#
export LANG=zh_TW.big5
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
accountfile="user.passwd"
# 1. 進行帳號相關的輸入先
echo ""
echo "例如我們崑山四技的學號為: 4960c001 到 4960c060"
echo "密碼例如: zxc26001 到 zxc26060echo , 那麼: "
echo "帳號開頭代碼為 :4"
echo "帳號層級或年級為 :960c"
echo "號碼數字位數為(001~060):3"
echo "帳號開始號碼為 :1"
echo "帳號數量為 :60"
echo "密碼開頭代碼 :zxc26
echo ""
read -p "帳號開頭代碼 ( Input title name, ex> std )======> " username_start
read -p "帳號層級或年級 ( Input degree, ex> 1 or enter )=> " username_degree
read -p "號碼部分的數字位數 ( Input \# of digital )======> " nu_nu
read -p "起始號碼 ( Input start number, ex> 520 )========> " nu_start
read -p "帳號數量 ( Input amount of users, ex> 100 )=====> " nu_amount
read -p "密碼標準 1) 與帳號相同 2)亂數自訂3)使用者自訂 ==============> " pwm
read -p "密碼開頭代碼 ( Input title name, ex> std )======> " pw_degree
if [ "$username_start" == "" ]; then
echo "沒有輸入開頭的代碼,不給你執行哩!" ; exit 1
fi
# 判斷數字系統
testing0=$(echo $nu_nu | grep '[^0-9]' )
testing1=$(echo $nu_amount | grep '[^0-9]' )
testing2=$(echo $nu_start | grep '[^0-9]' )
if [ "$testing0" != "" -o "$testing1" != "" -o "$testing2" != "" ]; then
echo "輸入的號碼不對啦!有非為數字的內容!" ; exit 1
fi
# 2. 開始輸出帳號與密碼檔案!
[ -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
nu_len=${#i}
if [ $nu_nu -lt $nu_len ]; then
echo "數值的位數($i->$nu_len)已經比你設定的位數($nu_nu)還大!"
echo "程式無法繼續"
exit 1
fi
nu_diff=$(( $nu_nu - $nu_len ))
if [ "$nu_diff" != "0" ]; then
nu_nn=000000000
nu_nn=${nu_nn:1:$nu_diff}
fi
account=${username_start}${username_degree}${nu_nn}${i}
if [ "$pwm" == "1" ]; then
password="$account"
elif [ "$pwn" == "2" ]; then
password=$(openssl rand -base64 6)
else
password=${pw_start}${pw_degree}${nu_nn}${i}
fi
echo "$account":"$password" | tee -a "$accountfile"
done
# 3. 開始建立帳號與密碼!
echo "帳密建置中,請稍待片刻!"
cat "$accountfile" | cut -d':' -f1 | xargs -n 1 useradd -m
chpasswd < "$accountfile"
pwconv
echo "OK!建立完成!"
但裡面還是有個bug 就是 如果你位數寫2
但如果用到10也就是說 一但到了十數位就會多一個0
例如
帳號開頭代碼為 :4
帳號層級或年級為 :960c
號碼數字位數為(001~060):
2帳號開始號碼為 :1
帳號數量為 :
10密碼開頭代碼 :zxc26
第一個帳號是4960c01
密碼是zxc2601
照道理說 第十個帳號因該是4960c10
不過 事實上是4960c
010
密碼也是zxc26010
如果能解bug的人謝謝你
如果不能了話 這個bug又對你沒影響 那就請用吧~~
用了請為我加油 下禮拜要去比賽~~
