akabou wrote:
Hello, everybody

Bonsoir,

I have a csv file with firstname, lastname, group

and need to create user and group if it doesn't exist.

But the problem is that i must not use useradd ou groupadd.

That way lies madness. If you do not intend to use the tools provided then how do you intend to preform this magic?


I must read user from csv, and then check if user exist,

perldoc -f getpwnam


and if group exist.

perldoc -f getgrnam


i've started like this, but i don't know how to check if user exist,
and group exist.

Here is the starting of my code.

$file = './liste_etudiants.csv';
open (FICH,$file) ||die ("Fichier innexistant");

You should at least include the $! variable in your error message so you know *why* the file failed to open.


while ($line = <FICH>)
{
        #on splie chauqe ligne
        ($field1,$field2,$field3) = split (',', $line);
        #on  enlève le charcatère "
        ($field1) =~ s/"//g;
        ($field1) = substr($field1, 0, 1);
        ($field2) =~ s/"//g;
        ($field3) =~ s/"//g;

Why not remove all the " first:

        $line =~ tr/"//d;
        my ( $field1, $field2, $field3 ) = split /,/, $line;


        ($login) = $field1 . $field2;
        print "$field1 : $field2 : $field3 -- le login sera login$login sera
membre de $field3 \n";
}

#ecrire dans un fichier
#open(F_WRITE,">./touche.txt") || die "E/S : $!\n";
#print $filed1;

close(FICH);

I wanted to test with tis kind of test

  open (FILE,"/etc/passwd");

You should *always* verify that the file opened correctly:

open FILE, '<', '/etc/passwd' or die "Cannot open '/etc/passwd' $!";


Or better yet, use one of the getpw* functions that are built in to Perl:

perldoc -f setpwent
perldoc -f getpwent
perldoc -f endpwent


  while () {

This creates an infinite loop, that, by the way, does not read from a file.


    chomp;

You are chomp()ing $_ which never gets set.


    /^([a-z][a-z0-9]*):x:([0-9]+):([0-9]+):/;

You are matching against $_ which never gets set. Why do you assume that the user name can only contain lower case letters and numbers? Why are you capturing three strings when you are only using one?


    #si le user id=user id dans le fichier on incrémante de 1 le uid
    if ($2 == $uid) { $uid++; }

You should let the tools provided by the operating system create UIDs (and GIDs and password hashes) for you.


  }

but don't know how.

I f someone could help me.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to