Linux--RH254---腳本作業

編寫 script.sh
1.
script.sh /mnt/userfile /mnt/passfile
2.
當要建立的用戶已經存在,不做任何操作
3.
當腳本後所指定文件個數少於2個
Please give me userfile or passfile
4.
當所給文件的行數不一致
userfile line and passfile line is different!


vim /mnt/script.sh
#!/bin/bash
while [ "$#" -lt "2" ]        ##檢測文件數目是否小於2
do
        echo "ERROR:Please give me userfile or passfile!"
        exit 0
done
MAXUSER=`wc -l $1 | cut -d " " -f 1`
MAXPASS=`wc -l $2 | cut -d " " -f 1`
while [ "$MAXUSER" -eq "$MAXPASS" ]    ##當所給文件的行數一致時
do
        for NUM in $( seq 1 $MAXUSER )
        do
                USERNAME=`sed -n ${NUM}p $1`
                PASSWORD=`sed -n ${NUM}p $2`
                CKUSER=`getent passwd $USERNAME`    ##檢測用戶是否存在
                [ -z "$CKUSER" ] && (
                useradd $USERNAME
                echo $PASSWORD | passwd --stdin $USERNAME &>/dev/null
                echo $USERNAME/$PASSWORD created successfully!
                )||echo "$USERNAME exist!"
        done
        exit 0
done

echo "$1's line and $2's line is different!"    ##當所給文件的行數不一致時



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章