Hello haukex,

I read your post so I guess something like that should be better :).

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use IPC::Run qw( run timeout ); sub rename_account { my ($in, $out, $err); my ( $name , $nname ) = ( @_ ); my @usename = ("usermod", "-l", "$nname", "$name"); run \@usename, \$in, \$out, \$err, timeout( 10 ) or die "Error: $? +"; say "OUT: $out" if $out; say "Error: $err" if $err; my @changeHomeDir = ("usermod", "-d", "/home/$nname", "-m", "$nnam +e"); run \@changeHomeDir, \$in, \$out, \$err, timeout( 10 ) or die "Err +or: $?"; say "OUT: $out" if $out; say "Error: $err" if $err; my @homeDir = ("ls", "-la", "/home"); run \@homeDir, \$in, \$out, \$err, timeout( 10 ) or die "Error: $? +"; chomp $out; return $out if $out; return "Error: $err" if $err; } say rename_account("monk", "testUser"); # say rename_account("testUser", "monk"); __END__ $ sudo perl test.pl total 12 drwxr-xr-x 4 root root 4096 Mar 21 19:52 . drwxr-xr-x 24 root root 4096 Mar 5 20:59 .. drwxr-xr-x 2 testUser testUser 4096 Mar 21 19:16 testUser

Update: Adding minor conversion STDOUT to array for better data manipulation.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use IPC::Run qw( run timeout ); sub rename_account { my ($in, $out, $err); my ( $name , $nname ) = ( @_ ); my @usename = ("usermod", "-l", "$nname", "$name"); run \@usename, \$in, \$out, \$err, timeout( 10 ) or die "Error: $? +"; say "OUT: $out" if $out; say "Error: $err" if $err; my @changeHomeDir = ("usermod", "-d", "/home/$nname", "-m", "$nnam +e"); run \@changeHomeDir, \$in, \$out, \$err, timeout( 10 ) or die "Err +or: $?"; say "OUT: $out" if $out; say "Error: $err" if $err; my @homeDir = ("ls", "-la", "/home"); run \@homeDir, \$in, \$out, \$err, timeout( 10 ) or die "Error: $? +"; my @data = split "\n", $out; return \@data if @data; return "Error: $err" if $err; } # print Dumper rename_account("monk", "testuser"); print Dumper rename_account("testuser", "monk"); __END__ $ sudo perl test.pl $VAR1 = [ 'total 12', 'drwxr-xr-x 4 root root 4096 Mar 22 10:46 .', 'drwxr-xr-x 24 root root 4096 Mar 7 15:04 ..', 'drwxr-xr-x 2 monk testuser 4096 Mar 22 10:43 monk', ];

Thank you for the information. BR / Thanos

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re^3: Linux::usermod - user name ... already exists by thanos1983
in thread Linux::usermod - user name ... already exists by mldvx4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.