problem of creating group for users

From: blur19755 (member29774_at_dbforums.com)
Date: 05/30/03

  • Next message: Wartan Hachaturow: "Re: Need people for full time positions"
    Date: Fri, 30 May 2003 04:22:23 +0000
    
    

    hi...
    i'm currently doing an assignment of making a program similar to
    the administrator of a unix system... i'm 'taking care' of the add
    user module...
    almost done with the codings... but i have a problem of adding
    random group for user... i also dunno how to create some embedded
    filees like .profile, .chsh... can anyone explain to me how to use
    chown and chgrp...

    pls pls... whoever have the knowledges...
    pls do share with me....
    thansk....

    this is my coding.....
    * ps: if u find any bugs in it... pls dun hesitate to change it....

    mkdir ptmp

    #add login

    echo "----ADD NEW USER TO LINUX SYSTEM----"
    checkLoop=""
    until [ "$checkLoop" = "true" ]
    do
            echo -n "Please Enter Login Name: "
            read loginName

            match=`grep -w "$loginName" /etc/passwd`

            if [ "$match" != "" ]
            then
                    echo "Login Name Already Exist,Please Enter A new
                    Login Name."

            elif [ `echo "$loginName" | grep -v '^[a-zA-Z0-9]'` ]
            then
                    echo "Invalid Character"

            elif [ `echo "$loginName" | grep \*` ]
            then
                    echo "Login Name Cannot Contain \"*\" !"

            elif [ `echo "$loginName" | wc -m` -gt 8 ]
            then
                    echo "Login Name Is Too Long!"

            elif [ `echo "$loginName" | wc -m` -lt 4 ]
            then
                    echo "Login Name Is Too Short!"

            else
                    checkLoop="true"
                    clear
            fi

    done

    #add group

    echo "----ADD NEW USER TO GROUP---"
    checkLoop=""
    until [ "$checkLoop" = "true" ]
    do

            echo "1) ADMINISTRATOR"

            echo "2) STUDENT"

            echo "3) OTHER GROUP"

            echo "4)BACK TO ADD NEW USER"

            echo -n "Please enter a choice:"
            read choice

            clear

            case "$choice" in

            1) GID=`grep admin /etc/group | awk -F: '{print $3}'`
                    checkLoop="true"
                    clear
                    ;;

            2) GID=`grep student /etc/group | awk -F: '{print $3}'`
                    checkLoop="true"
                    clear
                    ;;

            3) echo "Please enter name for your group"
                    read groupName

                    match=`grep -w "$groupName" /etc/group`

                    if [ "$match" != "" ]
                    then
                            GID=`grep "$groupName" /etc/group | awk -F:
                            '{print $3}'`
                            validLoop="true"

                            preGUSER=`grep "$groupName" /etc/group | awk -F:
                            '{print $4}'`
                            grep -v "$groupName" /etc/group > /etc/tempgroup
                            echo "$groupName::$GID:$preUSER,$loginName" >>
                            /etc/tempgroup
                            rm -fr /etc/group
                            cp /etc/tempgroup /etc/group
                            rm -fr /etc/tempgroup

                    else
                            echo "There is no \""$groupName"\" in
                            /etc/group. Do you want to create it? [Y/N]"
                            read validChoice

                            if [ "$validChoice" = "Y" -o "$validChoice" =
                            "y" ]
                            then
                                    until [ $validLoop = "true" ]
                                    do

                                            if [ `echo "$groupName" | grep
                                            -v '^[a-zA-Z0-9]'` ]
                                            then
                                                    echo "Invalid
                                                    Characters!!!"

                                            elif [ `echo "$groupName" |
                                            grep \*` ]
                                            then
                                                    echo "Group Name Cannot
                                                    Contain \"*\" !"

                                            elif [ `echo "$groupName" | wc
                                            -m` -gt 8 ]
                                            then
                                                    echo "Group Name Is
                                                    Too Long!"

                                            elif [ `echo "$groupName" | wc
                                            -m` -lt 3 ]
                                            then
                                                    echo "Group Name Is
                                                    Too Short!"

                                            else
                                                    validLoop="true"
                                                    lastGID=`sort -nt: +2 -3
                                                    /etc/group | tail -1 |
                                                    awk -F: '{print $3}'`
                                                    GID=`expr $lastGID + 1`
                                                    clear
                                                    echo "$groupName::$GID:-
                                                    $loginName" >>
                                                    /etc/tempgroup

                                            fi
                                    done

                            else

                                    clear
                                    rmdir ptmp
                                    sh main.sh
                                    exit
                            fi
                    fi
                    checkLoop="true"
                    clear
                    ;;

            4)
                    checkLoop="true"
                    rmdir ptmp
                    clear
                    sh add.sh
                    ;;

            *)
                    clear
                    echo "Invalid Entry!!!"
                    ;;

            esac

    done

    #add comments
    echo "----ADD COMMENTS TO NEW USER----"
    checkLoop=""
    until [ "$checkLoop" = "true" ]
    do
            echo "Please enter comments not more than 15 characters: "
            read comment
            if [ `echo "$loginName" | wc -c` -gt 15 ]
            then
                    echo "Sorry, your comments has exceeded 10 words!"

            else
                    checkLoop="true"
                    clear

            fi
    done

    lastUID=`sort -nt: +2 -3 /etc/passwd | tail -1 | awk -F: '{print $3}'`
    newUID=`expr $lastUID + 1`
    mkdir -p /home/$loginName
    chown -R $loginName /home/$loginName
    chgrp -R $groupName /home/$loginName
    dumPasswd=`grep "abc123" /etc/shadow
    echo "$loginName:$dumPasswd:$newUID:$GID:$comment:/$loginName:/bin/bash"
    >> /etc/passwd

    rmdir ptmp
    clear
    sh main.sh

    --
    Posted via http://dbforums.com
    

  • Next message: Wartan Hachaturow: "Re: Need people for full time positions"