random problem with a /n after a regexp

random problem with a /n after a regexp

am 16.09.2004 09:07:45 von Pierre-Yves

hello,

I have a problem in a perl script and I don't know how to solve it.
I'm trying to get the primary group of a unix user calling the unix command
id

id myuser
returns uid=12345(myuser) gid=54321(mygroup)

to get the group name I have written a regexp :
my $pri_grp = `id myuser`;
$pri_grp =~ s/^.+gid=.+\((.+)\)$/$1/;

sometimes it works fine and it returns the group name, sometimes it seems it
returns the group name followed by a RETURN (\n)

I tried to make a chomp on the variable but still I get the problem (not
everytime).

This prevent my script to work fine (I put the code below... it's a
recursive function that gets the owner of the parent directories and add a
new group to these users).

heeellllpppppp please !!!

PS: I'm using perl 5.8.2 on Solaris 5.8



#!/usr/bin/perl

my $parent_vapth = "/dev/thematics/dir1/dir12/dir121/dir1211";
my $th_apth = "/dev/thematics";
my $usr_group = "grp12111";

sub setGroups {
my $br_path = $_[0];
my $th_path = $_[1];
my $usr_grp = $_[2];
my $err_cnt = $_[3];

return false if $br_path !~ m#^${th_path}/.+$#;

# ls -od to get the owner of the directory
my $ls = `ls -od $br_path`;
my @ls = split(/\s+/,$ls);

# find primary group for user (using id)
my $pri_grp = `id $ls[2]`;
chomp($pri_grp); #test
$pri_grp =~ s/^.+gid=.+\((.+)\)$/$1/;
chomp($pri_grp); #test

### HERE I GOT A PROBLEM, IT SEEMS THAT SOMETIMES $pri_grp ENDS WITH
A '\n'
print "DEBUG: primary group is ***${pri_grp}***\n";

# list the groups the user belongs (using groups)
my $grp_lst = `groups $ls[2]`;
chomp($grp_lst);

# exclude the primary group and add the new group
@grp_lst = split(' ',$grp_lst);
$grp_lst = "";

foreach my $curr_grp (@grp_lst) {
next if $curr_grp eq $pri_grp;
$grp_lst .= "${curr_grp},";
}

$grp_lst .= "$usr_grp";

my $rc = system("usermod -G $grp_lst $ls[2]");

if ($rc ne 0) {
print "usermod -G $grp_lst $ls[2] failed. return code is
$rc\n";
}

# go one level up
$br_path =~ m#(.+)/.+$#;
$br_path = $1;

return setGroups($br_path, $th_path, $usr_grp, $err_cnt);
}

setGroups($parent_vpath, $thematics_path, $user_group, 0);

Re: random problem with a /n after a regexp

am 17.09.2004 02:36:18 von someone

Pierre-Yves wrote:
>
> I have a problem in a perl script and I don't know how to solve it.
> I'm trying to get the primary group of a unix user calling the unix command
> id

perldoc -f getgrnam
perldoc -f getgrgid
perldoc -f getgrent


> id myuser
> returns uid=12345(myuser) gid=54321(mygroup)
>
> to get the group name I have written a regexp :
> my $pri_grp = `id myuser`;
> $pri_grp =~ s/^.+gid=.+\((.+)\)$/$1/;
>
> sometimes it works fine and it returns the group name, sometimes it seems it
> returns the group name followed by a RETURN (\n)
>
> I tried to make a chomp on the variable but still I get the problem (not
> everytime).
>
> This prevent my script to work fine (I put the code below... it's a
> recursive function

perldoc File::Find


> that gets the owner of the parent directories and add a
> new group to these users).
>
>
> #!/usr/bin/perl
>
> my $parent_vapth = "/dev/thematics/dir1/dir12/dir121/dir1211";
> my $th_apth = "/dev/thematics";
> my $usr_group = "grp12111";
>
> sub setGroups {
> my $br_path = $_[0];
> my $th_path = $_[1];
> my $usr_grp = $_[2];
> my $err_cnt = $_[3];
>
> return false if $br_path !~ m#^${th_path}/.+$#;
>
> # ls -od to get the owner of the directory
> my $ls = `ls -od $br_path`;
> my @ls = split(/\s+/,$ls);

perldoc -f -o
perldoc -f stat


> # find primary group for user (using id)
> my $pri_grp = `id $ls[2]`;
> chomp($pri_grp); #test
> $pri_grp =~ s/^.+gid=.+\((.+)\)$/$1/;
> chomp($pri_grp); #test
>
> [snip]


John
--
use Perl;
program
fulfillment

Re: random problem with a /n after a regexp

am 17.09.2004 08:38:36 von Pierre-Yves

Hello,

Thank you... I did system call because I know unix much better than perl.
I didn't know about getpwnam and similar functions.
Now it works.


"John W. Krahn" wrote in message
news:6kq2d.37155$KU5.30719@edtnps89...
> Pierre-Yves wrote:
> >
> > I have a problem in a perl script and I don't know how to solve it.
> > I'm trying to get the primary group of a unix user calling the unix
command
> > id
>
> perldoc -f getgrnam
> perldoc -f getgrgid
> perldoc -f getgrent
>
>
> > id myuser
> > returns uid=12345(myuser) gid=54321(mygroup)
> >
> > to get the group name I have written a regexp :
> > my $pri_grp = `id myuser`;
> > $pri_grp =~ s/^.+gid=.+\((.+)\)$/$1/;
> >
> > sometimes it works fine and it returns the group name, sometimes it
seems it
> > returns the group name followed by a RETURN (\n)
> >
> > I tried to make a chomp on the variable but still I get the problem (not
> > everytime).
> >
> > This prevent my script to work fine (I put the code below... it's a
> > recursive function
>
> perldoc File::Find
>
>
> > that gets the owner of the parent directories and add a
> > new group to these users).
> >
> >
> > #!/usr/bin/perl
> >
> > my $parent_vapth = "/dev/thematics/dir1/dir12/dir121/dir1211";
> > my $th_apth = "/dev/thematics";
> > my $usr_group = "grp12111";
> >
> > sub setGroups {
> > my $br_path = $_[0];
> > my $th_path = $_[1];
> > my $usr_grp = $_[2];
> > my $err_cnt = $_[3];
> >
> > return false if $br_path !~ m#^${th_path}/.+$#;
> >
> > # ls -od to get the owner of the directory
> > my $ls = `ls -od $br_path`;
> > my @ls = split(/\s+/,$ls);
>
> perldoc -f -o
> perldoc -f stat
>
>
> > # find primary group for user (using id)
> > my $pri_grp = `id $ls[2]`;
> > chomp($pri_grp); #test
> > $pri_grp =~ s/^.+gid=.+\((.+)\)$/$1/;
> > chomp($pri_grp); #test
> >
> > [snip]
>
>
> John
> --
> use Perl;
> program
> fulfillment