LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-16-2017, 11:35 AM   #1
marcelo91
LQ Newbie
 
Registered: Jul 2017
Posts: 2

Rep: Reputation: Disabled
Exclamation How can I check if a group does not have users and delete it?


How can I check if a group does not have users and delete it?

I'm doing a bash linux script, and I need to delete a group with the groupdel command, but I have to validate that the group I'm going to remove is empty, which does not have users.

This is what I have done:

Code:
  bajagroup () {
printf "\ nEnter the name of the group to delete: \ n"
read -r remove group
[-n $ deletegroup] && groupdel $ deletegroup
if [$? -ne 0]; then
                 echo "The group was not deleted from the system. Please try again."
         else
                 echo "The group was deleted from the system."
fi
sleep 3
}
Something similar to the delgroup command with the --only-if-empty option, but with the groupdel command.

Example:
Code:
delgroup --only-if-empty
Thank you very much,
 
Old 07-16-2017, 11:46 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,622

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
your script is syntactically incorrect, therefore it will not run, or will not do what you want (if eventually will do anything).
please try www.shellcheck.net to check your shell script.

You need a space after the [ and before the ]
you must keep $ and variable "in one" space is not allowed in between.
what is remove group good for?

and in general I'm not really sure if it was really a good idea.
 
1 members found this post helpful.
Old 07-16-2017, 11:59 AM   #3
marcelo91
LQ Newbie
 
Registered: Jul 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
your script is syntactically incorrect, therefore it will not run, or will not do what you want (if eventually will do anything).
please try www.shellcheck.net to check your shell script.

You need a space after the [ and before the ]
you must keep $ and variable "in one" space is not allowed in between.
what is remove group good for?

and in general I'm not really sure if it was really a good idea.
OK thank you very much,

Now my question is, how can I check if a group is empty (has not users) to be able to delete it? If there are users and I want to delete it, an error appears.
 
Old 07-16-2017, 12:37 PM   #4
dejank
Member
 
Registered: May 2016
Location: Belgrade, Serbia
Distribution: Debian
Posts: 229

Rep: Reputation: Disabled
Code:
getent group groupname
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Secondary group users need to change the file permissions of primary group files? sunnybmv Linux - Newbie 3 12-10-2009 04:57 PM
Group quota not limiting for users not identical to the group? jalexxi Linux - Server 1 03-10-2009 12:44 AM
permissions not recognizing group or assigned users to group blckspder Linux - Server 1 07-28-2008 10:32 AM
Delete group users from /etc/passwd SCRIPT wesleywest Linux - General 5 03-08-2005 09:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration