FAQ
Hello,
I have a subroutine that uses useradd to create accounts

--
@cmd = ('useradd', '-m', $account);
my $result = system @cmd;
--

but when useradd fails, I need to stop it from sending the error message to
STDER.
Is it possible with system?

Thanks!

Search Discussions

  • Kevin Spencer at Jul 19, 2011 at 9:35 pm

    On Tue, Jul 19, 2011 at 12:14 PM, Tessio Fechine wrote:
    Hello,
    I have a subroutine that uses useradd to create accounts

    --
    @cmd = ('useradd', '-m', $account);
    my $result = system @cmd;
    --

    but when useradd fails, I need to stop it from sending the error message to
    STDER.
    Is it possible with system?

    Thanks!
    Just as you would when using the shell, redirect STDERR somewhere
    else: "2>/dev/null".

    Kevin.
  • Jim Gibson at Jul 19, 2011 at 11:22 pm
    On 7/19/11 Tue Jul 19, 2011 12:14 PM, "Tessio Fechine" <oissetf@gmail.com>
    scribbled:
    Hello,
    I have a subroutine that uses useradd to create accounts

    --
    @cmd = ('useradd', '-m', $account);
    my $result = system @cmd;
    --

    but when useradd fails, I need to stop it from sending the error message to
    STDER.
    Is it possible with system?
    You can use a shell process to discard STDERR messages (untested):

    my $cmd = "useradd -m $account 2> /dev/null";
    my $result = system($cmd);

    You could do the same by writing a shell script to redirect STDERR and call
    that from your Perl program.

    You can also use the IPC::Open3 module to capture STDERR. See 'perldoc
    IPC::Open3' for examples.
  • Shlomi Fish at Jul 20, 2011 at 9:43 am
    Hi Jim,

    On Tue, 19 Jul 2011 16:22:14 -0700
    Jim Gibson wrote:
    On 7/19/11 Tue Jul 19, 2011 12:14 PM, "Tessio Fechine" <oissetf@gmail.com>
    scribbled:
    Hello,
    I have a subroutine that uses useradd to create accounts

    --
    @cmd = ('useradd', '-m', $account);
    my $result = system @cmd;
    --

    but when useradd fails, I need to stop it from sending the error message to
    STDER.
    Is it possible with system?
    You can use a shell process to discard STDERR messages (untested):

    my $cmd = "useradd -m $account 2> /dev/null";
    my $result = system($cmd);
    The problem with interpolating strings into shell commands like that is that
    someone may put malicious code in it:

    my $account = 'foo; rm -fr / ';

    so be careful - see: http://shlomif-tech.livejournal.com/35301.html
    (Code/Markup Injection and Its Prevention ).

    Regards,

    Shlomi Fish
    You could do the same by writing a shell script to redirect STDERR and call
    that from your Perl program.

    You can also use the IPC::Open3 module to capture STDERR. See 'perldoc
    IPC::Open3' for examples.




    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

    An apple a day keeps the doctor away.
    Two apples a day will keep two doctors away.
    — one of Shlomi Fish’s relatives

    Please reply to list if it's a mailing list post - http://shlom.in/reply .
  • Christian Walde at Jul 20, 2011 at 7:46 am

    On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine wrote:

    Hello,
    I have a subroutine that uses useradd to create accounts

    --
    @cmd = ('useradd', '-m', $account);
    my $result = system @cmd;
    --

    but when useradd fails, I need to stop it from sending the error message to
    STDER.
    Is it possible with system?

    Thanks!
    For an easy and cross-platform solution you can use Capture::Tiny to capture both STDERR and STDOUT separately and then have your perl code decide what to do with each.

    --
    With regards,
    Christian Walde
  • C.DeRykus at Jul 21, 2011 at 1:14 am

    On Jul 20, 12:45 am, walde.christ...@googlemail.com ("Christian Walde") wrote:
    On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine wrote:
    Hello,
    I have a subroutine that uses useradd to create accounts
    --
    @cmd = ('useradd', '-m', $account);
    my $result = system @cmd;
    --
    but when useradd fails, I need to stop it from sending the error message to
    STDER.
    Is it possible with system?
    Thanks!
    For an easy and cross-platform solution you can use Capture::Tiny to capture both STDERR and STDOUT separately and then have your perl code decide what to do with each.
    Neat solution. (I wonder why the name 'Tiny' rather than...
    say, IPC::Capture for instance..)

    Another possibility would IPC::Run :

    use IPC::Run qw( run timeout );

    run \@cmd, \$in, \$out, \$err, timeout( 10 )
    or die "cmd err: $?";

    --
    Charles DeRykus

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 19, '11 at 7:14p
activeJul 21, '11 at 1:14a
posts6
users6
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase