redirect system command STDER

--0050450176cca585b404a870ebf6
Content-Type: text/plain; charset=ISO-8859-1

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

--
[at] cmd = ('useradd', '-m', $account);
my $result = system [at] cmd;
--

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

Thanks!

--0050450176cca585b404a870ebf6--
Tessio Fechine [ Di, 19 Juli 2011 21:14 ] [ ID #2062245 ]

Re: redirect system command STDER

On 7/19/11 Tue Jul 19, 2011 12:14 PM, "Tessio Fechine" <oissetf [at] gmail.com>
scribbled:

> Hello,
> I have a subroutine that uses useradd to create accounts
>
> --
> [at] cmd = ('useradd', '-m', $account);
> my $result = system [at] 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.




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mi, 20 Juli 2011 01:22 ] [ ID #2062286 ]

Re: redirect system command STDER

On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine <oissetf [at] gmail.com> wr=
ote:

> Hello,
> I have a subroutine that uses useradd to create accounts
>
> --
> [at] cmd =3D ('useradd', '-m', $account);
> my $result =3D system [at] cmd;
> --
>
> but when useradd fails, I need to stop it from sending the error messag=
e to
> STDER.
> Is it possible with system?
>
> Thanks!
>

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

--
With regards,
Christian Walde

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Christian Walde [ Mi, 20 Juli 2011 09:45 ] [ ID #2062287 ]

Re: redirect system command STDER

On Tue, Jul 19, 2011 at 12:14 PM, Tessio Fechine <oissetf [at] gmail.com> wrote:
> Hello,
> I have a subroutine that uses useradd to create accounts
>
> --
> [at] cmd = ('useradd', '-m', $account);
> my $result = system [at] 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.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kevin Spencer [ Di, 19 Juli 2011 23:35 ] [ ID #2062288 ]

Re: redirect system command STDER

Hi Jim,

On Tue, 19 Jul 2011 16:22:14 -0700
Jim Gibson <jimsgibson [at] gmail.com> wrote:

> On 7/19/11 Tue Jul 19, 2011 12:14 PM, "Tessio Fechine" <oissetf [at] gmail.c=
om>
> scribbled:
>
> > Hello,
> > I have a subroutine that uses useradd to create accounts
> >
> > --
> > [at] cmd =3D ('useradd', '-m', $account);
> > my $result =3D system [at] cmd;
> > --
> >
> > but when useradd fails, I need to stop it from sending the error messag=
e to
> > STDER.
> > Is it possible with system?
>
> You can use a shell process to discard STDERR messages (untested):
>
> my $cmd =3D "useradd -m $account 2> /dev/null";
> my $result =3D system($cmd);
>

The problem with interpolating strings into shell commands like that is that
someone may put malicious code in it:

my $account =3D '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 ca=
ll
> 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.
=E2=80=94 one of Shlomi Fish=E2=80=99s relatives

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mi, 20 Juli 2011 11:42 ] [ ID #2062289 ]

Re: redirect system command STDER

On Jul 20, 12:45=A0am, walde.christ... [at] googlemail.com ("Christian
Walde") wrote:
> On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine <oiss... [at] gmail.com> wr=
ote:
> > Hello,
> > I have a subroutine that uses useradd to create accounts
>
> > --
> > [at] cmd =3D ('useradd', '-m', $account);
> > my $result =3D system [at] cmd;
> > --
>
> > but when useradd fails, I need to stop it from sending the error messag=
e to
> > STDER.
> > Is it possible with system?
>
> > Thanks!
>
> For an easy and cross-platform solution you can use Capture::Tiny to capt=
ure both STDERR and STDOUT separately and then have your perl code decide w=
hat 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 \ [at] cmd, \$in, \$out, \$err, timeout( 10 )
or die "cmd err: $?";

--
Charles DeRykus




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Do, 21 Juli 2011 03:08 ] [ ID #2062412 ]
Perl » gmane.comp.lang.perl.beginners » redirect system command STDER

Vorheriges Thema: first post
Nächstes Thema: XML suggestions