酷!學園

技術討論區 => Linux 討論版 => 主題作者是: rita299 於 2020-11-28 10:33

主題: bash script- create a user and a group
作者: rita2992020-11-28 10:33
想請教如何寫一個BASH SCRIPT create a user and a group(if the group is not exist)

#!bin/bash
read -p "Enter USERNAME: " userNAME
read -p "Enter USERGROUP name: " userRGROUP

grep $USERGROUP /etc/group 2>&1> /dev/null

while [ $? -eq 0 ]
        read -p "Enter USERNAME: " userNAME
        read -p "Enter USERGROUP name: " userRGROUP
grep $USERGROUP /etc/group 2>&1> /dev/null
        printf "Group Name you entered $USERGROUP is not valid\n"
        printf "Creating Abort!"\n"
        exit 1
do
        useradd -g $USERGROUP -d /home/$USERNAME -s /bin/bash -m $USERNAME
        passwd $USERNAME
done

id $USERNAME

printf "Done!\n"

exit 0