424,066 Members | 2,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,066 IT Pros & Developers. It's quick & easy.

Why does this Bash script not work?

P: 6
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 modifyUserAssociation {
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 "^${username}:" /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 "^${username}:" /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
}
2 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.