首先把要建立的賬號以及密碼在一個資料文本, 每一行有兩個欄位:
例如:
john iLoveMary
mary iHateJohn
jason mmmmmm
maggie iAmLonely
第一位為 username,
第二位為 password,兩個欄位位間以一或多個空白隔開。
Script 的內(nèi)容如下:
#!/bin/bash
echo -n " Give me the name of the file containing user data..."
read file
[ ! -f $file ] && ( echo " '$file' does not exist..."; exit 1 )
while read username password
do useradd $username >/dev/null 2>&1
if [ $? -ne 0 ]
then echo " Fail to create an account with name=$username..."
else echo $password | passwd --stdin $username >/dev/null 2>&1
[ $? -ne 0 ] && echo " Fail to set password for '$username'..."
fi
done < $file