By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,490 Members | 3,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,490 IT Pros & Developers. It's quick & easy.

Why does this Bash script not work?

P: 8
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
}
Jan 3 '19 #1

✓ answered by libreusl

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.)

Share this Question
Share on Google+
1 Reply


libreusl
P: 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.)
Mar 28 '19 #2

Post your reply

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