2011年12月26日 星期一

撰寫程式 產生10名使用者

建立使用者名單 users.txt

 $touch users.txt

 $nano users.txt

把10名使用者及密碼寫上(可自行設定)

a001:student
a002:student
a003:student
a004:student
a005:student
a006:student
a007:student
a008:student
a009:student
a010:student


編寫自動產生使用者的程式,名字為 useradd.sh

$touch useradd.sh

$chmod +x useradd.sh

$nano useradd.sh

編寫程式如下:

[ ! -f users.txt ] && echo "file not found" && exit 1
 
 
s=$(cat users.txt)
 
 
for  us  in $s
do
    uname=${us%%:*}
 
 
    upasswd=${us##*:}
 
 
    useradd -m -s /bin/bash $uname
    echo "$uname:$upasswd" | chpasswd

    mkdir  /home/$uname/www
    chown "$uname:$uname" /home/$uname/www

    echo "$uname home" >  /home/$uname/www/index.html
    chown "$uname:$uname"  /home/$uname/www/index.html
 
done


執行程式
$sudo ./useradd.sh

沒有留言:

張貼留言