LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2004, 05:14 AM   #1
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Rep: Reputation: 30
Red face Simple script for adding groups to Linux


Howdy!

I am trying to write a script for adding groups and users for my samba domain... Here is what I have tried writing, I have not started on the user part since I cannot get the groups to work properly:

Code:
#!bin/bash

echo -n "Enter group name: "
read group_name

#checks to see if group is already added to /etc/group 
if [ "$group_name" == "grep '$group_name' /etc/group" ]; then
    echo "Group already exists! Skipping group creation..."
    exit 1
else
    /usr/sbin/groupadd $group_name
    echo "Group added"
fi
Output:
# sh grouptest.sh
Enter group name: test
Group added

#sh grouptest.sh
Enter group name: test
groupadd: group test exists
Group added

Seem to me the the portion just below the if statement is entirely ignored... Why is that?
I have programming basics, just not good at it thats all

Cheerio~!
 
Old 05-27-2004, 10:24 AM   #2
Oliv'
Senior Member
 
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36
Hello,

It seems that your IF statement is always wrong even if the group already exists
For example, if you type:
Code:
grep root /etc/group
root:x:0:
the result is the whole line with hidden password and GID... so to correct it either you use a "|cut" extension to have only group name or you find another way like:
Code:
if [$(grep $group_name $group_file | wc -l) -eq 1]
Hope this help you
Oliv'
 
Old 05-27-2004, 10:42 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Well it won't work if an user has the same name of a group for example.
I would do :
Code:
group_test=$(cat /etc/group | awk -F: '{ print $1 }' | grep $group_name)

if [ -z $group_test ]; then
    -- add the group
else
     -- print group exist
fi
 
Old 05-27-2004, 10:52 AM   #4
Oliv'
Senior Member
 
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36
Yes, you're right keefaz But it was only an example... a wrong one
But the solution with the "cut" command is good as you tell the script to keep the first word of the line
Code:
grep $group_name $group_file | cut -d: -f1
Oliv'
 
Old 05-27-2004, 11:06 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
We are false. Try with a group named "roo"
so I revisit my example

Code:
group_test=$(cat /etc/group | awk -F: '{ print $1 }' | grep $group_name)

if [ -z $group_test ] || [ ! "$group_test" = "$group_name" ]; then
    -- add the group
else
     -- print group exist
fi
 
Old 05-27-2004, 06:49 PM   #6
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Original Poster
Rep: Reputation: 30
I'm not exactly sure what you 2 experts are writing... but it works!!
Thanks a million~!

I spent the whole day looking at scpriting websites but i'm still in the dark about all these: ' " ` { [ (

OK, now time for me to understand what all those terms mean in the code... do I look at man bash?

Any websites that you think would be good for a beginnner?

Thanks again keefaz and Oliv'

chEErs,
~WiLL~
 
Old 05-27-2004, 07:08 PM   #7
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48
http://www.ss64.com/bash/
 
Old 05-27-2004, 09:15 PM   #8
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Original Poster
Rep: Reputation: 30
Quote:
We are false. Try with a group named "roo" so I revisit my example

Code:
group_test=$(cat /etc/group | awk -F: '{ print $1 }' | grep $group_name)

if [ -z $group_test ] || [ ! "$group_test" = "$group_name" ]; then
    -- add the group
else
    -- print group exist
fi
OK... let me confirm a few things:
- The commands inside $(...|...|...) greps the first column (before the first colon) of /etc/group which matches the entered group_name, right? then assigns the value to group_test...
- then the first if condition checks if group_test returns a null, right?

More questions on expressions...
- $(...|...|...), whats the use of the $ and | sign?
- for anything within "...", the value stored in that variable is compared?
- whats the difference if I use [ "$group_test" != "$group_name" ] instead?
- also difference between = and ==

Geez... i ask to much...
Thanks a lot~!

Thanks for the link 320mb~!
 
Old 05-27-2004, 10:14 PM   #9
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Original Poster
Rep: Reputation: 30
Forgot another simple one; is there any difference between:
/usr/sbin/groupadd $group_name
and
/usr/sbin/groupadd "$group_name"

Thank you all~!
 
Old 05-28-2004, 08:59 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
There is no difference between /usr/sbin/groupadd $group_name and /usr/sbin/groupadd "$group_name". But when in test case the "" are needed to extrapolate the value from the variable.

For the question whats the use of the $ and | sign?, the $ sign means the result will be put in a variable and the | is a pipe symbol. It is a useful command line concept. It take the result of the previous command (before the | ) and work with it.
Example :
echo "Hello" | wc -c
echo "Hello" # print Hello
| wc -c # take Hello and display its number of char
You can use
[ "$group_test" != "$group_name" ]
or [ ! "$group_test" = "$group_name" ]
it is the same.

From the man bash :
"= may be used in place of == for strict POSIX compliance"
so you can use '=' or '==' for testing strings

I am not a bash expert but your prob interested me also I doubt that the script will be usefull as the command groupadd will prevent you if a group already exist.

Last edited by keefaz; 05-28-2004 at 09:21 AM.
 
Old 05-29-2004, 09:58 AM   #11
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Original Poster
Rep: Reputation: 30
Thanks a million, keefaz~!
That really cleared a lot of questions off my mind

I was actually trying to write a menu type of script to add groups, users and machine accounts

Why can't I create a directory after the group add command?
Code:
/usr/sbin/groupadd $group_name
mkdir /home/$group_name
i got an error that sounds like, cannot create directory

Something wrong with the syntax?
 
Old 05-29-2004, 10:18 AM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
do you run the script as root ? Maybe fix /home permissions
If home permissions are not
drwxr-xr-x 6 root root
do chmod 755 /home as root
 
Old 05-29-2004, 08:39 PM   #13
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Original Poster
Rep: Reputation: 30
Mine's:
drwxr-xr-x 3 root root
By the way, i kind of forgot what the integer between the permission and ownership means, mind refreshing me on that?

I am root... thats why I was surprised why I can manually create a directory but not using a script...
 
Old 05-29-2004, 11:32 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
For the integer between permission and ownership in ls -l, do ls -a in a directory, count the directory files, then cd.. and do ls -l, you should see the same number in the second field of output ls -l ( between permission and ownership ).

I think your $group_name variable is not initialised (no value) so It can't create directory.
 
Old 05-30-2004, 12:02 AM   #15
kenji1903
Member
 
Registered: Apr 2004
Location: M'sia, Aus, Chn
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301

Original Poster
Rep: Reputation: 30
Thanks bro

Umm... initialising it... I thought its initialised to the group name entered by the user?
 
  


Reply



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
viewing, adding(to), deleting(from) groups chibi Linux - Newbie 6 10-16-2005 08:38 PM
Adding more groups to files djchris Linux - Security 1 07-27-2005 10:36 AM
Simple groups question scottrell Linux - General 1 12-02-2004 03:35 PM
Adding user and groups manon Linux - Newbie 3 04-26-2004 12:14 PM
Adding secondary groups thepurpleblob Linux - General 1 09-03-2003 10:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

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