Why does this Bash script not work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ZS369
    New Member
    • Aug 2018
    • 8

    Why does this Bash script not work?

    Hi! the following function doesn't work and i've tried to figure out what the problem is but can't figure it out. thank you in advance for any help!

    function modifyUserAssoc iation {
    echo "Do you want to add a user to a group (1) or delete a user from a group (2)?"
    read option

    if [[ $option == 1 ]];
    then
    echo "Enter username"
    read username
    echo "Enter group name"
    read group
    fi

    if grep -q "^${usernam e}:" /etc/passwd && grep -q "^${group}: " /etc/group;
    then
    sudo usermod -a -G $group $username
    else
    echo "Invalid username and group!"
    fi

    if [[ $option == 2 ]];
    then
    echo "Which user would you like to delete?"
    read user
    echo "And from which group?"
    read group

    if grep -q "^${usernam e}:" /etc/passwd && grep -q "^${group}: " /etc/group;
    then
    gpasswd -d $username $group
    else
    echo "Invalid username and group!"
    fi
    fi

    if [[ $option != 1 && $option != 2 ]];
    then
    echo "Invalid input!"
    fi
    }
  • libreusl
    New Member
    • Mar 2019
    • 2

    #2
    For one, you ask if someone wants to add a user, then you say invalid username and group unless both the username (which you want to add) and the group are already found.

    That's like asking if you want to create a file, then asking for a filename, and refusing to create it unless it is already there.

    This is assuming that the rest of your if statement is properly constructed, but really it would be better to explain specifically what the script is doing (apart from what you want it to do.)

    Comment

    Working...