473,508 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does this Bash script not work?

8 New Member
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
1 2594
libreusl
2 New Member
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

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

Similar topics

5
2974
by: Phil Powell | last post by:
I'm sorry but I can't figure out how to explain this any better than this. In PHP we have a command "require()" that obtains a file and logically places it into another file. I cannot figure...
1
1953
by: Gonzalo Aguirre | last post by:
hi.. as i write on the topic, i'm programing a Bash script using the standard GNU Linux text utilities (gawk, grep, sed, and so on) that get information from SQL source (table attib, type per...
3
1839
by: Steve Richter | last post by:
How does the following javascript work? After the first script runs the variables "sGeobytesCity", "sGeobytesRegion" and "sGeobytesCountry" contain values that have been set somehow by the...
2
1772
by: benfbox-google | last post by:
Hi, there. The following javascript is working perfectly with IE, but does not work with Firefox (no error, but displays nothing). Can somebody help ? Thanks in advance, Ben. The script :
1
1490
by: alexxxprog | last post by:
Thank you very much for who can solve this bash script: "Create on the filesystem a bash proceedure createdata that creates collections that that will have to contain a variable, a random number,...
1
1812
by: pnjbi | last post by:
Hi, I've created a bash script which is executed every 5 minutes through a cron. The script checks if a trigger variable has been set to execute certain commands. If it is the commands are...
3
3845
by: Gros Bedo | last post by:
Hello :-) I have a question about Python and Linux shell. I have a python program which is permanently resident in the end-user system. I'm currently producing a RPM package, and it works nicely....
1
2972
by: Gros Bedo | last post by:
Yes I've seen that each python script calls its own instance of Python. Buthow to know which is the good one in bash ? Is there a command that gets the parameters of process, so I could use grep to...
0
1998
by: norseman | last post by:
Gros Bedo wrote: ============================== Yes. man ps explains try ps -AFL | grep then kill -9 found (check it more than twice) 1) If your script is known to hang use what...
0
3697
by: kidko | last post by:
I've written a Zenity frontend for ffmpeg using a Bash script. However, if the user hits "cancel" in the progress dialog (incurring the --auto-kill option), the bash script is killed. Unfortunately,...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.