Home > Archive > AWK > March 2004 > Newbie. Trouble with an Easy script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Newbie. Trouble with an Easy script
|
|
| Puckaroo 2004-03-26, 11:09 pm |
| 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
| |
| Brian Inglis 2004-03-30, 2:30 pm |
| 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
|
|
|
|
|