Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Newbie. Trouble with an Easy script
Hi, I've been trying to write a tiny script to add a line to the
/etc/group, it is suppoused to add the line only if the group_id is
not in the file yet.

This is what i've written, but it adds the line everytime even if the
group_id is there. I guess it's got something to do with the if but i
don't know what exactly.

Lines in the /etc/group file are like this:

So any idea on what i'm doing wrong?. Thanx a lot. Bbye.

groupname:x:group_id:

#./bin/bash

#addgroup <group_id>

awk 'BEGIN{FS=":";j=0}
{
if ($3=="'$1'")
then
j=1;
fi
}
END{
if (j==0)
then
printf("group%d:x:%d:\n","'$1'","'$1'");
fi
}'<group  >>group

Report this thread to moderator Post Follow-up to this message
Old Post
Puckaroo
03-27-04 04:09 AM


Re: Newbie. Trouble with an Easy script
On 24 Mar 2004 04:48:55 -0800 in comp.lang.awk, Pukaroo@hotmail.com
(Puckaroo) wrote:

>Hi, I've been trying to write a tiny script to add a line to the
>/etc/group, it is suppoused to add the line only if the group_id is
>not in the file yet.
>
>This is what i've written, but it adds the line everytime even if the
>group_id is there. I guess it's got something to do with the if but i
>don't know what exactly.
>
>Lines in the /etc/group file are like this:
>
>So any idea on what i'm doing wrong?. Thanx a lot. Bbye.
>
>groupname:x:group_id:
>
>#./bin/bash
>
>#addgroup <group_id>
>
>awk 'BEGIN{FS=":";j=0}
>{
>  if ($3=="'$1'")
>  then
>    j=1;
>  fi
>}
>END{
>     if (j==0)
>     then
>       printf("group%d:x:%d:\n","'$1'","'$1'");
>     fi
>   }'<group  >>group

if ... then ... fi are shell constructs, not awk, which uses the C
approach:

if ($3=="'$1'")
j=1;

if (j==0)
printf("group%d:x:%d:\n","'$1'","'$1'");

and it looks like then and fi are just being ignored as null
expressions.

Groups are expected to have names as well as ids and * is the normal
placeholder for the password field. Try this as a basis (untested):

#! /bin/bash
#  addgroup name id users
#	name:*:id:user,...

awk -vNAME="$1" -vID="$2" -vUSERS="$3" '
BEGIN      { OFS = FS = ":" }
$1 == NAME { found = $3 }
$3 == ID   { found = $3 }
$3 > max   { max = $3 }
END        {
if (found == "")
{
if (ID == "")	ID	= max + 1
if (NAME == "")	NAME	= "group" ID
print NAME, "*", ID, USERS
}
}
' < group  >> group

--
Thanks. Take care, Brian Inglis 	Calgary, Alberta, Canada

Brian.Inglis@CSi.com 	(Brian dot Inglis at SystematicSw dot ab dot ca)
fake address		use address above to reply

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Inglis
03-30-04 07:30 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:25 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.